0
public class B {
    private int x;
    public void foo() {
            int current = x;
            x = current + 1;
    }
    public void go() {
            for(int i = 0; i < 5; i++) {
                    new Thread() {
                            public void run() {
                                    foo();
                                    System.out.print(x + ", ");
                            } 
                    }.start();
            } 
    }
    public static void main(String args[]){
            B bb = new B();
            bb.go();
    }
}

This Code is from SCJP. i am getting confused in understanding the logic. What i understand is , this code has 5 threads (1 in main and 4 in loop) but i dont know how i am getting this output : 3, 5, 3, 4, 3,

Although i know output varies from machine to machine but i want to understand hows threads are been shuffled in between

Regards

xlm
  • 6,854
  • 14
  • 53
  • 55
user2985842
  • 437
  • 9
  • 24

3 Answers3

1

I'm not sure what all the Java rules about execution order are; but I think that in the absence of synchronization, operations can be executed in any order, and "operations" don't have to be entire statements. Suppose you break the code down into four operations: read x and set current (in foo); assign to x (in foo); read x (in run), into some temporary; concatenate the value of x (from the temporary) with a comma and print it. Here's an order which could cause the output you're seeing, but which I believe follows the Java rules in that the operations within each thread are executed in the correct order. Note that each thread has its own value of current (while in foo()), but x is shared by all threads.

Thread 1: Read x into current (0)
Thread 1: Set x to 1
Thread 2: Read x into current (1)
Thread 2: Set x to 2
Thread 3: Read x into current (2)
Thread 3: Set x to 3
Thread 1: Read x into temporary (3)
Thread 2: Read x into temporary (3)
Thread 3: Read x into temporary (3)
Thread 4: Read x into current (3)
Thread 4: Set x to 4
Thread 4: Read x into temporary (4)
Thread 5: Read x into temporary (4)
Thread 5: Set x to 5
Thread 5: Read x into temporary (5)
Thread 3: Print x (3)
Thread 4: Print x (4)
Thread 2: Print x (3)
Thread 5: Print x (5)
Thread 1: Print x (3)

Of course, this isn't the only possible order that would produce that output.

ajb
  • 31,309
  • 3
  • 58
  • 84
0

the 5 threads share the x variable which belongs to the "bb" object. Hence when their run method is executed, execution can switch from 1 thread to the other at any point. Hence when they print, some other threads might have gone further than others, or have not started at all...

Add prints with the thread id at every line of the run & foo methods to see how they interlace.

XSen
  • 188
  • 1
  • 2
  • 9
  • good luck with that... adding `print` will probably change the whole order around. Debugging concurrency can be fun!! – ajb Mar 11 '14 at 22:02
-1

There are 6 Threads, 1 in main and 5 in loop

hmashlah
  • 144
  • 7