JavaBeat
calling cards | international calling cards | phone card
JAVABEAT
home
articles
tips
QnA
forums
QNA TOPICS
All api java
java 6.0 java 7.0 jdbc
jsp servlet ejb
jndi jms ejb 3.0
j2ee jee 5.0 jee 6.0
jsf struts spring
Hibernate ajax JBoss Seam
netbeans eclipse ant
xml maven dojo
junit javafx j2me
log4j ESB JBoss
Apache Quartz scjp
mysql oracle gwt
openjpa jmx yui
google-guice android JBoss
scwcd 5.0 scjp 1.5 scjp 1.6
scja scbcd 5.0
JavaBeat Feeds JavaBeat RSS Feeds for Articles and Tips
JavaBeat Articles
JavaBeat QnA

JavaBeat Tips

Links for Developers

When we start a thread by applying start() method on it ,how does it knows that to execute run()method?

Why do we need start() method in Thread class? In Java API description for Thread class is written : "Java Virtual Machine calls the run method of this thread..".

Couldn't we call method run() ourselves, without doing double call: first we call start() method which calls run() method? What is a meaning to do things such complicate?

There is some very small but important difference between using start() and run() methods. Look at two examples below:

Example one:

Code:

Thread one = new Thread();
Thread two = new Thread();
one.run();
two.run();

Example two:

Code:

Thread one = new Thread();
Thread two = new Thread();
one.start();
two.start();

The result of running examples will be different.

In Example one the threads will run sequentially: first, thread number one runs, when it exits the thread number two starts.

In Example two both threads start and run simultaneously.

Conclusion: the start() method call run() method asynchronously (does not wait for any result, just fire up an action), while we run run() method synchronously - we wait when it quits and only then we can run the next line of our code.


Favorites
AffiliatedAds.com
Buy movies
Access Control
Busby seo challenge contest
Sohbet
Chat
Webmaster Hosting Forum
Java Jobs
MyVideoLib
India News
Internet Advances
Sohbet
chat
Latest QnA
SCJD Tips
When we start a thread by applying start() method on it ,how does it knows that to execute run()method?
About Wrapper class in Java
How to configure weblogic 7.0 in MyEclipse?
Static Block and Static Initializer in Java

JavaBeat Website (2004-2008), India
javabeat | about us | planetoss
Copyright (2004 - 2008), JavaBeat