为什么线程之间共享静态变量 [英] Why static variable is shared among the threads

查看:381
本文介绍了为什么线程之间共享静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我读过的几乎所有帖子中-

In almost all the posts I have read -

如果两个线程(假设线程1和线程2)正在访问同一对象并更新声明为静态的变量,则意味着线程1和线程2可以对同一对象进行自己的本地复制(包括静态变量) )在其各自的缓存中,因此线程1将其更新为本地缓存中的静态变量不会反映在线程2缓存的静态变量中.在对象上下文中使用静态变量,其中一个对象的更新将反映在同一类的所有其他对象中,而在线程上下文中则不使用,在该上下文中,将一个线程更新为静态变量将立即反映所有线程的更改(在他们的本地缓存).

If two Threads(suppose Thread 1 and Thread 2) are accessing the same object and updating a variable which is declared as static then it means Thread 1 and Thread 2 can make their own local copy of the same object(including static variables) in their respective cache, so updating by Thread 1 to the static variable in its local cache wont reflect in the static variable for Thread 2 cache . Static variables are used in the Object Context where updating by one object would reflect in all the other objects of the same class but not in the Thread context where updating of one thread to the static variable will reflect the changes immediately to all the threads (in their local cache).

但是当我在下面的代码段中运行

But when I run below 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();
            }
        }
    }
}

输出为-

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

由于线程之间没有共享静态变量,因此对于线程2,测试值应始终为1.但这不是事实.

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

我阅读了许多与同一个问题相关的其他问题,但仍然无法理解为什么会这样.有人可以帮我理解这个问题.

I read so many other questions related to same problem but still I am not able to understand why is this happening. Could someone please help me in understanding this problem.

谢谢.

推荐答案

我认为您对内存模型的危险感到困惑.

I think you're being confused by the dangers of the memory model.

静态变量 -您正在阅读的文章并未试图说每个线程都有自己独立的一组静态变量.相反,他们一直试图通知您,如果不设置适当的内存屏障,则不能保证未经保护的共享状态修改在其他线程中是可见的.可以立即将这些更改显示给其他线程,这是完全可以的-只是没有保证.

Static variables are shared between threads - the articles you've been reading haven't been trying to say that each thread has its own independent set of static variables. Instead, they've been trying to inform you that unprotected modification of shared state isn't guaranteed to be visible in other threads without setting up appropriate memory barriers. It's entirely fine for those changes to be visible to other threads immediately - it's just not guaranteed.

这篇关于为什么线程之间共享静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆