Mayank Tiwari
Mayank Tiwari

Reputation: 3020

Why i am getting this output in this java Thread program?

Hello i am a beginner in java programming, recently i am studying Threads, i am having problem in output of this program.

class s1 implements Runnable 
{ 
    int x = 0, y = 0; 
    int addX() {x++; return x;} 
    int addY() {y++; return y;} 
    public void run() { 
        for(int i = 0; i < 10; i++){ 
            System.out.println(addX() + " " + addY()); 
        }
    } 
    public static void main(String args[]) 
    { 
        s1 run1 = new s1(); 
        s1 run2 = new s1(); 
        Thread t1 = new Thread(run1); 
        Thread t2 = new Thread(run2); 
        t1.start(); 
        t2.start(); 
    } 
}

I am getting output like this,

1 1 2 2 1 1 3 3..., please explain why?

Upvotes: 2

Views: 380

Answers (3)

Joe
Joe

Reputation: 47609

Each instance of the s1 class has its own variables, so they will increment independent of each other. If you only made one instance, the output would be 1 1 2 2 3 3 ....

If you take two threads each printing 1 1 2 2 3 3 ..., you will see the two streams mixed up. As long as it outputs the correct number of each number, in the right order, it is doing what you expect. You cannot expect how the threads will be scheduled.

So, you might see 1 1 2 2 3 3 1 1 2 2 3 3... or 1 1 1 1 2 2 2 2 3 3 3 3... or any other variation.

(You might even get lucky and see 1 1 1 1 2 2 2 2 3 3 3 3 ..., one day, if the scheduler slices in a certain way)

EDIT: Also read this answer on thread-safety within the println call.

Upvotes: 5

darijan
darijan

Reputation: 9775

Try executing this code:

class Test extends Thread { 

    Test(String name) {
        super(name);
    }

    int x = 0, y = 0; 
    int addX() {x++; return x;} 
    int addY() {y++; return y;} 

    public void run() { 
    for(int i = 0; i < 10; i++) 
        System.out.println(addX() + " " + addY() + ", name:"  + getName()); 
    }


    public static void main(String args[]) { 
        Test run1 = new Test("thread1"); 
        Test run2 = new Test("thread2"); 
        run1.start(); 
        run2.start(); 
    } 
}

You will get output similar to this one:

1 1, name:thread2
2 2, name:thread2
1 1, name:thread1
2 2, name:thread1
3 3, name:thread2
3 3, name:thread1

That is because treads do not execute synchronously. You don't know when will one be executed. In your code 1 1 and then later again 1 1 is just the output of two threads doing the same thing.

Upvotes: 0

Michael Berry
Michael Berry

Reputation: 72284

The threads are executing asynchronously - so their output will be naturally intertwined, that's to be expected. In your case:

1 1 2 2 1 1 3 3

...The bit I've "bolded" is the output from one thread, the bit I've left plain is the (start of) the output from the other. I can only work this out because of how the program executes - if you had two threads just printing the character "1" for example, it would be impossible to distinguish what thread was printing what character.

Note that the order in which the numbers appear and the way they intertwine is completely arbitrary - it could have just as easily been something like:

1 1 1 1 2 2 3 3 2 2..

...Or any other possible combination. Don't rely on the order that you happen to get for any particular program, it's completely undefined.

Upvotes: 7

Related Questions