This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "thread" Watch "thread" New topic
Author

thread

Sahil Manchanda
Ranch Hand

Joined: Jul 24, 2012
Posts: 41

please anybody explain why the answer is not guaranteed to be 2000

Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 721
one.join() and two.join means let one and two finish before the main thread.
I think one and two will concurrently execute the run method and concurrently execute increase() as increase() is not synchronized.
Here is a possibility:
one: number = 0
two: number = 0
one: attempt to increase number . but number is not set to 1 in the memory yet.
two: increase number, set number = 1 in memory.
one: set number to 1 as a result of its previous step.
and so on....

I think when a number is increased by a thread, the operating system does these 3 steps in machine code:
1. put a copy of number to temporary memory
2. increase the number in this temp memory
3. copy the new value in step to and put it back to the original number's memory location.

I am not sure if that is the case.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: thread
 
Similar Threads
nikos thread question
Niko's mock exam question
Threads
Question on Thread - from Niko's mock
Threads