Java中的线程安全全局变量

use*_*247 7 java multithreading thread-safety

我试图了解java中的线程安全机制,我需要一些帮助.我有一节课:

public class ThreadSafe {

    private Executor executor = new ScheduledThreadPoolExecutor(5);

    private long value = 0;

    public void method() {
        synchronized (this) {
            System.out.println(Thread.currentThread());
            this.value++;
        }
    }

    private synchronized long getValue() {
        return this.value;
    }

    public static void main(String... args) {
        ThreadSafe threadSafe = new ThreadSafe();
        for (int i = 0; i < 10; i++) {
            threadSafe.executor.execute(new MyThread());
        }

    }

    private static class MyThread extends Thread {

        private ThreadSafe threadSafe = new ThreadSafe();

        private AtomicBoolean shutdownInitialized = new AtomicBoolean(false);

        @Override
        public void run() {
            while (!shutdownInitialized.get()) {
                threadSafe.method();
                System.out.println(threadSafe.getValue());
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

在这里,我试图使value线程安全,一次只能由一个线程访问.当我运行这个程序时,我发现value即使我将它包装在synchronized块中,也有多个线程在运行.当然这个循环将是无限的,但它只是一个例子,我几秒后手动停止这个程序,所以我有:

2470
Thread[pool-1-thread-3,5,main]
2470
Thread[pool-1-thread-5,5,main]
2470
Thread[pool-1-thread-2,5,main]
Run Code Online (Sandbox Code Playgroud)

不同的线程正在访问和更改它value.有人可以向我解释为什么会这样吗?以及如何使这个全局变量线程安全?

ysh*_*vit 10

每个线程都有自己的ThreadSafe,每个ThreadSafe都有自己的不同value.此外,synchronized方法锁定this,因此每个方法ThreadSafe都锁定自身 - 并且它们之间没有任何共享.这称为线程局部性,它是确保线程安全的最简单方法.:)

为了得到我认为你想要的实验,你需要改变MyThread它的构造函数接受一个ThreadSafe参数(而不是构造一个).然后,让main方法创建一个ThreadSafeMyThread在构造时将其分配给每个方法.