Threads question ocpjp.v7.2.1730

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
shareef.hiasat
Posts: 20
Joined: Thu Dec 19, 2013 8:22 am
Contact:

Threads question ocpjp.v7.2.1730

Post 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); 
    }
      }

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Threads question ocpjp.v7.2.1730

Post 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.
If you like our products and services, please help us by posting your review here.

shareef.hiasat
Posts: 20
Joined: Thu Dec 19, 2013 8:22 am
Contact:

Re: Threads question ocpjp.v7.2.1730

Post 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

shareef.hiasat
Posts: 20
Joined: Thu Dec 19, 2013 8:22 am
Contact:

Re: Threads question ocpjp.v7.2.1730

Post 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 .

runnerdave
Posts: 12
Joined: Mon Jan 30, 2017 2:58 pm
Contact:

Re: Threads question ocpjp.v7.2.1730

Post 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()?

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Threads question ocpjp.v7.2.1730

Post 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.
If you like our products and services, please help us by posting your review here.

runnerdave
Posts: 12
Joined: Mon Jan 30, 2017 2:58 pm
Contact:

Re: Threads question ocpjp.v7.2.1730

Post 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?

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Threads question ocpjp.v7.2.1730

Post by admin »

Yes, that is correct.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 55 guests