jQuery in Action, 3rd edition
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "thread" Watch "thread" New topic


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: 1122
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
It's not a secret anymore!