首页 文章

Java中的线程安全全局变量

提问于
浏览
7

我试图了解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());
            }
        }
    }

}

在这里,我试图使 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]

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

2 回答

  • 10

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

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

  • 4

    每次都获得相同的值,因为每个 Runnable 都有自己的 ThreadSafe 类实例 .

    如果你希望他们共享同一个类,那么你将只需要一个 ThreadSafe 的实例并将其传递给你的所有工作 - 见下文 . 如上所述,如果你想要一个线程安全的共享 long ,那么 AtomicLong 就是你的选择 .

    另外,你的 MyThread 课不应该 extend Thread . 它应该改为 implements Runnable . 您的代码正在运行,因为 Thread 已经 implements Runnable . 如果你做了 myThread.interrupt() 它实际上不会中断线程,因为它是调用你的 run() 方法的线程池线程 .

    像下面这样的东西会起作用:

    ThreadSafe threadSafe = new ThreadSafe();
    for (int i = 0; i < 10; i++) {
        threadSafe.executor.execute(new MyRunnable(threadSafe));
    }
    ...
    private static class MyRunnable implements Runnable {
        private final ThreadSafe threadSafe;
        public MyRunnable(ThreadSafe threadSafe) {
           this.threadSafe = threadSafe;
        }
        ...
    

相关问题