Page 1 of 1

Threads question ocpjp.v7.2.1730

Posted: Fri Aug 22, 2014 9:22 am
by shareef.hiasat
Why this question the answer is
It may print any number between -5000 to 5000.
the explanation is
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

Code: Select all

 
public class RunTest {    
 
public static volatile int counter = 0;     
static class RunnerDec implements Runnable{ 
        
public void run(){             
for(int i=0;i<5000; i++){
                 counter--;             }
         }
     } 
     static class RunnerInc implements Runnable{
         public void run(){
             for(int i=0;i<5000; i++){
                 counter++;             }
         }
     } 
         public static void main(String[] args) {
         RunnerDec rd = new RunnerDec();
         RunnerInc ri = new RunnerInc();
         Thread t1 = new Thread(rd); 
        Thread t2 = new Thread(ri);
         t1.start();
         t2.start(); 
        try{
             t1.join();
             t2.join();
         }catch(Exception e){ 
            e.printStackTrace();
         }
         System.out.println(counter); 
    }
      }

Re: Threads question ocpjp.v7.2.1730

Posted: Fri Aug 22, 2014 10:08 am
by admin
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.

Re: Threads question ocpjp.v7.2.1730

Posted: Fri Aug 22, 2014 11:55 am
by shareef.hiasat
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

Re: Threads question ocpjp.v7.2.1730

Posted: Fri Aug 22, 2014 2:27 pm
by shareef.hiasat
shareef.hiasat wrote:
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 :D the question you have provided the second solution is what i wanted .

Re: Threads question ocpjp.v7.2.1730

Posted: Mon Feb 20, 2017 5:40 am
by runnerdave
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.

Can you please explain just the join()?

Re: Threads question ocpjp.v7.2.1730

Posted: Mon Feb 20, 2017 7:19 am
by admin
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.

Re: Threads question ocpjp.v7.2.1730

Posted: Mon Feb 20, 2017 2:57 pm
by runnerdave
thanks a lot, so to always print 0, if I put the join() between the start() calls like this:

t1.start();
t1.join();
t2.start();
t2.join();

Can I assume it will always print 0 then?

Re: Threads question ocpjp.v7.2.1730

Posted: Mon Feb 20, 2017 9:54 pm
by admin
Yes, that is correct.