Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

The run() method and thread lifecycle

 
Ranch Hand
Posts: 40
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 73738
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried it several times and got

java TestMyThread
Back to main!!Hi from MyThread

each time.
 
This tiny ad is guaranteed to be gluten free.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic