This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
calling run() instead of start() while running THREAD
Harish Tiruvile
Ranch Hand
Joined: Dec 01, 2005
Posts: 99
posted
0
Hi all, why we are calling threadInstance.start() method to run a thread even though we can invoke run() method directly by calling threadInstance.run()
...i have wrote one code and checked the output i observed one strange thing...it is, we can invoke run() method withod using start() method..... so what is the significance of using threadInstance.start() method over threadInstance.run() method....
I hope something is wrong here ..but i am unable to trace that because both codes is executing properly...
public class Threadtest {
public static void main(String args[]) { new Threadtest(); }
Threadtest() { Thread t1 = new Thread(new First()); Thread t2 = new Thread(new Second()); t1.start(); t2.start(); }
}
class First implements Runnable {
public void run() { for (int i=0;i<2;i++) { System.out.println("first thread run() is invoked");
try{ Thread.sleep(100); }catch(Exception e){} } }
}
class Second implements Runnable {
public void run() { for (int i=0;i<2;i++) System.out.println("snd thread run() is invoked");
} } ************output for above code *********************** case 1:when i use t1.start() , t2.start()
first thread run() is invoked snd thread run() is invoked snd thread run() is invoked first thread run() is invoked
case 2: when i use t1.run(),t2.start()
first thread run() is invoked first thread run() is invoked snd thread run() is invoked snd thread run() is invoked ****************************************************** Only difference i am finding(thinking) is sleep() is not invoked........is that true if so please tell me the reason...if it is wrong then please tell me the significance of using threadInstance.start() method over threadInstance.run() method.... [ May 08, 2006: Message edited by: harish thrivile ]
Giving up is the easiest thing in the world to do..but holding it together when everything seems like falling apart is true strength!!
with regards, Harish.T
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35247
7
posted
0
Just because something can be done doesn't mean it should be done. Your code should call start(), which in turn will cause the JVM to invoke run(). Read the javadocs to learn about cases where calling run directly will not work. [ May 08, 2006: Message edited by: Ulf Dittmer ]
You can call run() like any other method, but like any other method it will execute on the caller's thread. It's just plain code at that point.
start() on the other hand does some deep magic - probably down to native OS code - to create a new thread of execution and call run() on that thread.
Most often when you create a Runnable object you want it to run on another thread. You might do that by creating and starting a Thread or by passing your Runnable to a thread pool or Timer or something like that.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
Originally posted by harish thrivile: Only difference i am finding(thinking) is sleep() is not invoked........is that true if so please tell me the reason...if it is wrong then please tell me the significance of using threadInstance.start() method over threadInstance.run() method....
Just to clarify on what the other two wrote: sleep *is* in fact invoked, but it's only after third.run() is finished that second.run() is even invoked.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Harish Tiruvile
Ranch Hand
Joined: Dec 01, 2005
Posts: 99
posted
0
Ilja Preuss ,
Just to clarify on what the other two wrote: sleep *is* in fact invoked, but it's only after third.run() is finished that second.run() is even invoked.
sir i am not getting your above statement can you please elaborate it...
Even if i increase the sleep time no change occured in the output(After completing First class run(),Secound class run() is invoked irrespective of sleep duration )...ofcourse it is impossible to predict which thread will execute and how long it will execute .... from this can i conclude 1) t1.run() wont create a new thread for execution 2) t1.start() will create a thread
please correct me if i am wrong
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
You are correct.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: calling run() instead of start() while running THREAD