• 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

Thread execution

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


The output is:

before the thread is started: 10
The value of the integer variable is: 10


Why value is not increemented second time?
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably, you were not very lucky....
As soon as you call whiz.start(); a new thread comes to life (other one is your main thread). Now, in your case, before the new thread could reach value++; thread scheduler gave chance to the main thread to run, and the main thread completed the stmt ' System.out.println("The value of the integer variable is: "+Temp.value);' before value is actually incremented.
Again, this may not be the case always.
Just try and put Thread.sleep(100); after whiz.start() and you'll see the difference. Hope that clears.
 
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because, Before Second Thread( whiz.start(); ) starts and runs, Main method ends.

Try this, this works,

 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Being specific to the problem,

whiz.run() will not create a new execution thread.Calling the run method will simply execute the target(in the run() mehod of the Thread class),which in this case is null.So calling of this method will have no effect.
whiz.start() will create a new execution thread.But it is upto the scheduler who is going to decide the time to execute the new target... the value of the value may or may not increment after this method call.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prabu,

Can you please explain what to do to make sure that the main thread finshes its execution only after the thread in the code???
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sanjeev Kumar Singh: "Calling the run method will simply execute the target(in the run() mehod of the Thread class),which in this case is null.So calling of this method will have no effect."

The above is wrong. Calling whiz.run() will execute the run method of the Whiz instance referenced by whiz. So the instance variable value is incremented and printed. The thing is, it is done in the context of the main thread not the newly created thread.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic