Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!

Mahesh Perera

Greenhorn
+ Follow
since Oct 02, 2012
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
1
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Mahesh Perera

Hi,

I think, this is because of int values assignment is atomic. So that line 8 always executes as one instruction.So this programme does not produce output "P Q P Q".

But, it may produce the output "R S R S", because when two threads execute, whichever thread that executes first will set id=0 and may be preempted, allowing other thread to start.
Then second thread will set id=1. After that, in whatever order these two threads excute, there is no way to set id again to 0. So id=1 will remain. Therefore both threads will execute the
release() method.Since it is synchronized on object st, one thread has to wait until the other releases lock. So, output will be "R S R S". Because of this, I think we cannot say that there
is no chance for the same method to be executed twice
.
.

Correct me if I am wrong.

6 years ago