Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

calling run() instead of start() while running THREAD  RSS feed

 
Harish Tiruvile
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Harish Tiruvile
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!