Why static variable is shared between threads

In almost all the posts I have read -

If two threads (suppose threads 1 and threads 2) have access to the same object and an updatable variable that is declared static, then this means Thread 1 and Thread 2 can make their own local copy of the same object (i.e. static variables) in their respective cache, so updating on thread 1 of a static variable in its local cache does not reflect the static variable for the L2 cache. Static variables are used in the context of an object, where updating with one object will reflect in all other objects of the same class, but not in the context of a topic, where updating one thread to a static variable will reflect the changes to all threads at once (in their local cache).

But when I run the code snippet

public class StatciVolatile3 {

    public static void main(String args[]) {
        new ExampleThread2("Thread 1 ").start();
        new ExampleThread2("Thread 2 ").start();
    }

}

class ExampleThread2 extends Thread {
    private static int testValue = 1;

    public ExampleThread2(String str) {
        super(str);
    }

    public void run() {
        for (int i = 0; i < 3; i++) {
            try {
                System.out.println(getName() + " : " + i);
                if (getName().compareTo("Thread 1 ") == 0) {
                    testValue++;
                    System.out.println("Test Value T1: " + testValue);
                }
                if (getName().compareTo("Thread 2 ") == 0) {
                    System.out.println("Test Value T2: " + testValue);
                }
                Thread.sleep(1000);
            } catch (InterruptedException exception) {
                exception.printStackTrace();
            }
        }
    }
}

      

Output -

Thread 1  : 0
Thread 2  : 0
Test Value T2: 2
Test Value T1: 2
Thread 2  : 1
Test Value T2: 2
Thread 1  : 1
Test Value T1: 3
Thread 2  : 2
Test Value T2: 3
Thread 1  : 2
Test Value T1: 4

      

Since the static variable is not shared between threads, so for thread 2, the test value should always be 1. But this is not the case.

I have read so many other questions related to the same problem, but still I cannot figure out why this is happening. Can someone please help me in understanding this problem.

Thanks in advance.

+3


source to share


2 answers


I think you are confused by the danger of the memory model.



Static variables are shared between threads - the articles you read did not try to say that each thread has its own independent set of static variables. Instead, they were trying to tell you that an unprotected modification of the shared state is not guaranteed to be visible on other threads without setting appropriate memory barriers. It's great if these changes are visible to other lows right away - it's just not guaranteed.

+5


source


There is no guaranteed time interval when a Java implementation is required to make changes made by one thread visible to another thread without synchronization or a mutable modifier.

The idea is to give different JVM developers a choice (because their implementation has to run on different hardware in different contexts) about how aggressively they want to optimize their work with caching, instruction reordering, etc. If you are comparing the Oracle x86 JVM to ARMv7, they make very different choices.



The posted code is an example of a toy. In real life it would be better to use parallel collections or message passing. By relying on shared global state in the form of static variables, you talk about how you could have done this.

+1


source







All Articles