• Post Reply Bookmark Topic Watch Topic
  • New Topic

The run() method and thread lifecycle  RSS feed

 
Shouvik Bhattacharya
Ranch Hand
Posts: 40
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys!!

One small question.........as soon as the run() method is over, as in the final curly brace of run() method is reached is it true to that the control almost immediately gets back to the main() thread. Although it can return but it may return say after 30 sec.......is this not possible.



So, based on the above snippet the "Back to main" may be the output almost immediately as soon as run() is done with its execution or it might also be possible that it gets printed 30 sec after run() is done with its job.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouvik Bhattacharya wrote:as soon as the run() method is over, as in the final curly brace of run() method is reached is it true to that the control almost immediately gets back to the main() thread.
So, based on the above snippet the "Back to main" may be the output almost immediately as soon as run() is done with its execution or it might also be possible that it gets printed 30 sec after run() is done with its job.

No. Control can return to the main thread as soon as the start method completes.
It's possible that Back to main!! will be printed before Hi from MyThread.

It is highly unlikely that it would ever take 30 seconds for Back to main!! to be printed.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as soon as the run() method is over, as in the final curly brace of run() method is reached is it true to that the control almost immediately gets back to the main() thread. A


The control remains in the main() thread while executing the start() instruction. It does not go to any other thread. It is the main thread that executes the t.start(); instruction and it is the main thread that executes the System.out.print("Back to main!!); instruction. The main thread starts another thread but still it is the main thread only that was executing when the other thread was started. The run() method of every Thread would be executed in the Thread in which the run method belongs. In other words, if there is true concurrency ( meaning you can have two threads executing in parallel ), main thread could be executing the System.out.println instruction along with the other thread running in its run method. The other thread, just like main thread, is an independent execution unit with its own program stack and counter. The other thread does not even require main() to be alive while it is running and the vice versa.

So, based on the above snippet the "Back to main" may be the output almost immediately as soon as run() is done with its execution or it might also be possible that it gets printed 30 sec after run() is done with its job.

When you are dealing with threads, such things cannot be said with absolute accuracy. It is likely that "Back to main" will be printed immediately after main has completed executing the t.start(); instruction cause that instruction is the next instruction to be executed in the main thread. If however, there is a context switch after the main thread executes the t.start(); instruction, it could take a while. But 30 seconds is a long time and like I said such things cannot be predicted.

 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it several times and got
java TestMyThread
Back to main!!Hi from MyThread
each time.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!