wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on  Thread - from Niko's mock 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 "Question on  Thread - from Niko Watch "Question on  Thread - from Niko New topic
Author

Question on Thread - from Niko's mock

Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115
Here are three program(all are same), but with subtle changes,

1)These classes are defined in a single file. There’s a value object Account that carefully protects it’s state from concurrent access, a Client class of type Thread that puts some money in an account, and a main method that simply starts two clients.

Just before the main method exits, the account’s number field is guaranteed to have value 2000.

Answer: false

2)These classes are defined in a single file.
Just before the main method exits, the account’s number field is guaranteed to have value 2000.

Answer: true

3)These classes are defined in a single file.
Just before the main method exits, the account’s number field is guaranteed to have value 2000.

Answer: false

When i compile all three programs, "2000" is printed in the line, "//here" for all the programs.
so why different answers?


SCJP 6.0 90%
Rajeev Trikha
Ranch Hand

Joined: Jan 29, 2010
Posts: 85
There is lost update problem in the two scenarios which are giving false. Change 1000 to 100000 and you will see the problem more clearly.


Rajeev Trikha (SCJP 6)
Rajeev Trikha
Ranch Hand

Joined: Jan 29, 2010
Posts: 85
I notice Michal B on Niko's blog has given clear explanation here to these questions His post is near the bottom.
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Hi

This method is accessed in both programs simultaneously by two threads:

but in the first program this method is synchronized, and it is not synchronized in the second one.
Synchronized - it means that at the same time one - and only one thread can execute number++ instruction
- and therefore result is guaranteed to be 2000.
In the second example this is not true - both treads could enter this method and simultaneously execute number++,
and therefore result is not guaranteed to be 2000 ('not guaranteed' means that it in most cases it will be 2000,
but sometimes, in very rare cases could be different than 2000)

Incremental operator is not an atomic operation (what atomic/not atomic means - you can find in the web).
The compiler compiles ++ operator into machine code that looks something like that:
1 load number value to register
2 increment register by 1
3 store result back to number

Now, consider that on multiprocessor machine two threads (on two different processors) execute this code precisely at the same time interval
(assume that number value is 231):
proc1: load number value to register (proc1/reg = 231)
proc2: load number value to register (proc2/reg = 231)
proc1: increment by 1 (proc1/reg = 232)
proc2:increment by 1 (proc2/reg = 232)
proc1:store result back to number (number = 232)
proc2:store result back to number (number = 232)

Here, number is 232, but should be 233 because it was incremented twice.
It is called 'memory consistency error' - code synchronization helps prevent this kind of errors.
Look here for details: http://java.sun.com/docs/books/tutorial/essential/concurrency/memconsist.html
Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115
thanks both of you..

Rajeev, i will look into the explanation in Niko's blog.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Thread - from Niko's mock