Making variable counter volatile only ensures that if it is updated by one thread, another thread will see the updated value. However, it still does not ensure that the increment and decrement operations will be done atomically. Thus, it is possible that while one thread is performing counter++, another thread corrupts the value by doing counter--. Because of this corruption, it is not possible to determine the final value of counter.
Am not understanding the explanation properly i think that because of join finally it will increment by 5000 and then decrement by 5000 after all so the answer is 0 or some one please explain to me slowley thanks
This is an important topic and if you are unable to understand the explanation then you need to go through a good book to first understand what is race condition and how it happens in Java. Then you will understand this question very easily.
You might want to start here: http://stackoverflow.com/questions/3451 ... -condition
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
admin wrote:This is an important topic and if you are unable to understand the explanation then you need to go through a good book to first understand what is race condition and how it happens in Java. Then you will understand this question very easily.
You might want to start here: http://stackoverflow.com/questions/3451 ... -condition
HTH,
Paul.
Thanks for replying , am aware of data race problem but this case confuses me , i mean
if the variable counter is static shouldnt
t1.join(); means the for of increment 5000 times is complete true or false
and
t2.join(); means the for of decrement of 5000 times is complete
admin wrote:This is an important topic and if you are unable to understand the explanation then you need to go through a good book to first understand what is race condition and how it happens in Java. Then you will understand this question very easily.
You might want to start here: http://stackoverflow.com/questions/3451 ... -condition
HTH,
Paul.
Thanks for replying , am aware of data race problem but this case confuses me , i mean
if the variable counter is static shouldnt
t1.join(); means the for of increment 5000 times is complete true or false
and
t2.join(); means the for of decrement of 5000 times is complete
now i understand the question you have provided the second solution is what i wanted .
Hi, I understand the concept of race conditions now, as well as the fact that volatile does not mean synchronization, but still don't understand why the join() calls don't ensure that the execution of the first thread finishes before the second commences.
The main thread is calling join on t1 and t2. Therefore, the join condition is between main and t1 (and then main and t2). There is no ordering between t1 and t2. It is the main thread that will pause until t1 finishes. It will then pause for t2 to finish. It is possible that t2 finishes before t1 because t2 doesn't join on t1.
If you like our products and services, please help us by posting your review here.