2

I have a simple Java program with 3 threads, each incrementing and then printing a variable counter. The java code is-

public class test implements Runnable {

    private int counter=0;

    public static void main(String args[]){

        test t=new test();
        Thread t1=new Thread(t,"THREAD 1");
        Thread t2=new Thread(t,"THREAD 2");
        Thread t3=new Thread(t,"THREAD 3");
        t1.start();
        t2.start();
        t3.start();

    }

    public  void run(){

        counter=counter+1;
            System.out.println(Thread.currentThread().getName()+" "+ counter);

    }
}


Understanding that the method run is not synchronized the threads can interleave in any fashion giving any result but the result I get mostly(I've run the program multiple times) is-

THREAD 1 2
THREAD 3 3
THREAD 2 2


I don't understand how come 1 is never printed and even after printing 3, 2 is again printed(since the most recent value of counter has been set to 3, as it is printed).

Also, is the variable counter shared among the 3 threads? It will be shared if I make it static but what in this case.
Please explain.

2
  • 1
    You're passing the same instance of t into your 3 threads, hence they all use the same counter instance. Also, the field counter is being modified by multiple threads and has no synchronization, so your computations are probably only cpu-memory and not stored in actual memory, which can lead to funky results Commented Jul 15, 2016 at 10:09
  • @RobinJonsson so in general class variables will be shared only if I pass the same instance of the object while creating threads, or else if I'm using static otherwise they will get on separate thread stack ? Commented Jul 15, 2016 at 10:12

2 Answers 2

2

First of all, the instance variable is shared by all the threads, because they are sharing the same instance of the runnable class (in you code there is only one time "new test()").

Why the the value 1 is never printed? This is a little more complex to answer because the order of the instructions is determined by the JVM implementation, the OS and the hardware (in general it is unpredictable). In your case it seems that the print function is called after 2 increment instructions. (if you think that printing is slower than incrementing a variable it take sense).

Finally, value 2 can be printed after value 3, because the println function is not synchronized, this means that also the instruction inside the print function can be interleaved. So in this case the print function on the 2nd thread is called before the function on the 3rd thread, but the first that finished is the 3rd thread.

I hope this can help you.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes it did. Thanks alot :D .
1

If you use

    test tOne=new test();
    test tTwo=new test();
    test tThree=new test();
    Thread t1=new Thread(tOne,"THREAD 1");
    Thread t2=new Thread(tTwo,"THREAD 2");
    Thread t3=new Thread(tThree,"THREAD 3");
    t1.start();
    t2.start();
    t3.start();

the output will be something like

THREAD 2 1
THREAD 1 1
THREAD 3 1

counter was shared because you used the same instance of t in all three threads.

7 Comments

Oh, I got it now. Thanks. And what about the print stuff. Value 1 never gets printed, and how come 2 came again after counter has been set to 3.
@Brut3Forc3 Which value is seen by the threads is unpredictable because there is no synchronization. That's the whole point why you need synchronization.
@Fildor I just tried using static keyword for counter variable but the output still seems almost the same.
I've tested your original code and sometimes the "1" was printed. In a run where the 1 is not printed, the incrementation of counter is finished in some Thread before another Thread starts to read it for printing it. But that behaviour is not predictable.
Oh, Thanks. Also, if I create threads by extending Thread class then will be able to create multiple threads of same instance as I'll need to use the new keyword everytime before creating a thread.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.