user2985842
user2985842

Reputation: 457

Threads in inner class

This code has no inner classes:

class Threads1 implements Runnable { 

    int x = 0; 

    public void run(){ 
        int current = 0; 
        for(int i = 0; i<4; i++){  
            current = x; 
            System.out.println(current + ", ");
            x = current + 2; 
        }  
    }  
} 

public class b{ 

    public static void main(String[] args) {
        Runnable r1 = new Threads1();
        new Thread(r1).start(); 
        new Thread(r1).start(); 
    } 
}

OUTPUT :

0, 2, 0, 4, 6, 2, 4, 6

This code uses an inner class called "Runner":

public class Threads1 { 
    int x = 0; 
    public class Runner implements Runnable {
        public void run(){  
            int current = 0; 
            for(int i = 0; i<4;i++){ 
                current = x;  
                System.out.println(current + ", "); 
                x = current + 2;  
            }
        }
    }

    public static void main(String[] args) {  
        new Threads1().go();
    }

    public void go(){
        Runnable r1 = new Runner();
        new Thread(r1).start();
        new Thread(r1).start();
    }
}

OUTPUT : (0, 2, 4, 4, 6, 8, 10, 6,) or (0, 2, 4, 6, 8, 10, 12, 14,)

I learned that when two threads are created , they work on their own stacks, which means they share nothing with each other i.e output ( 0, 2, 0, 4, 6, 2, 4, 6) might be from ( T1, T1, T2, T1, T1 ,T2, T2, T2) where T1 AND T2 are Thread 1 and Thread 2.

However , when I used run() in the inner class, both threads share the Current variable with each other. For example output (0, 2, 4, 4, 6, 8, 10, 6,) might be from (T1 ,T1, T1 ,T2 ,T2, T2, T2, T1). As you can see, there is double a 4 in the output which means thread1 handed over its value to thread2. Why is that so?

Upvotes: 0

Views: 1225

Answers (4)

x4rf41
x4rf41

Reputation: 5337

they work on there own stacks which means they share nothing with eachother

That is wrong. The threads do have a different stack, but they share the heap (that is the memory where all object instances and their member variables are, only function variables lie on the stack) and int x exists only once on the heap since you only create one Threads1 object, and both Runnables share it

you should move int x to the Runner class and create a new Runner object for every thread.

when i used run() in inner class both thread shares "Current" variable with eachother

also wrong, they actually share the int x variable, as already explained. current is unique for every thread

Upvotes: 1

CodeChimp
CodeChimp

Reputation: 8154

In the first case, x is a member of the Threads1 class, but Threads1 is instantiated individually by the b clas and thus each thread gets it's own x. In the second case, x is a member of your outer Threads1, and thus only one instance of x is created.

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533660

Your examines explore behaviour which is not defined. Threads accessing the same field, in a thread unsafe manner may or may not see the same values.

In the first example, the field x is being cached as if it were a local variable. There is no guarentee this behaviour would occur on different version of the JVM or after the code has been compiled.

In the second case you have an extra level of indirection which means the JIT is less likely to optimise the access of x into a local variable. However, again subtle changes could see a different behaviour.

Upvotes: 3

Taylor
Taylor

Reputation: 4086

Because x is shared, it's in the containing class.

Upvotes: 0

Related Questions