Mahesh Perera

Greenhorn
+ Follow
since Oct 02, 2012
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

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.

12 years ago