• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem On using wait() in THreads

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator





OUtPUT

Waiting for b to complete...
K
Total is: 4950

Queries:

1.I think When the code reaches: line 5 i.e.
b.start();
it should go straight away call the run Method of ThreadB,but it continue to the line following b.start();

why so ?

2.Is it Possible that main THread may be run at later time and THreadB earlier than main THread ?

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

QuoteYourSources
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, this question from K&B's.....?

mohitkumar gupta wrote:
1.I think When the code reaches: line 5 i.e.
b.start();
it should go straight away call the run Method of ThreadB,but it continue to the line following b.start();

why so ?



There are some procedure in the start() method of the Thread, so at the mean time the main Thread continues its work. So you'd reached the later parts of the program.

mohitkumar gupta wrote:
2.Is it Possible that main THread may be run at later time and THreadB earlier than main THread ?



Possible, but it's rare! Let's see what Henry say!
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,Mohit.After the 5th line ,the thread of b also becomes runnable but there is no gurantee that JVM will select this thread That's upto the scheduler.In other JVM or even in another execution of program,the output may change .If you want a specific thread to run you can influence JVM by methods like sleep,setPriority etc.


So ,the main point is even if you start a thread that doesn't mean it will be in running state.It comes into runnable state and after that which thread executes for how much time is upto scheduler.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




notice that in line no 7 when a thread is start....it continues executing the code....it will not trasfer its call
immediately to run method of ThreadB.....

so in order to get the right result and to make sure that the run method of class B get executed correctly.....
we have to synchronize the ThreadB object and call wait on it....
wait is always put in a try/catch block because it throws checked InterruptedException.....


you can remove this block


but in that case output will be zero
as after b.start().


compiler immediately reaches the line(System.out.println("Total is: " + b.total);
) and prints the value of total which will be zero because may be run()
method of threadB has not been started till yet......

try this code and see the output.........it may be different on your machine because output of threads are unpredictable but on my machine it is zero

 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//Is it Possible that main THread may be run at later time and THreadB earlier than main THread ?
yes it is possible put this within a static block


now the code will be like this




output is :--
Waiting for b to complete...
K
Total is: 4950
Main Thread
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Phil i am not able to understand how does static change this ?
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

harmeet saini wrote:
Hi Phil i am not able to understand how does static change this ?



see the below code everything will be clear.................




when we run any class files the execution starts from static method......
JVM first searches the All static method and run them........and after that when it starts the main
the main execution of the program starts......
see in the above code first all the static block executes then when JVM reaches the Main method then it will
create a object and run its contructor........but before running any construction it will see if there is any annonymous block is there
then before running the default constructor..it will run its anonymous block

So, when we create any object........
first anonymouse block executes and then default constructor....
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic