在多个线程之间传递 POJO(无易失性字段,同步方法)并确保更改可见性的正确方法?

Correct way to pass a POJO (no volatile fields,synchronized methods) between multiple threads and assure visibility of changes?

我发现了很多与可见性、同步和线程相关的问题和答案,但 none 似乎涵盖了我的特定用例(或者也许我只是不擅长搜索 ;-) 所以我我会问一个新问题,希望一些慷慨的灵魂能启发我:)

我的问题是:给定下面的代码,在主线程中访问 WorkItem 项的字段是否会正确反映线程池工作线程对它们所做的任何更改?

我的怀疑是 "NO" ,因为这感觉类似于传递一些值的数组并且只是在数组引用上同步而不是在单个元素上同步......必须有一个原因 类 像 AtomicReferenceArray 存在于 JDK 中,以及为什么它们在访问单个元素时使用 getVolatile()/setVolatile()。

package com.voipfuture.voipmng.monitoring;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class IsThisThreadSafe
{
    public static class WorkItem { public String field1,field2; /* etc. */ }

    public static void main(String[] args) throws InterruptedException
    {
        final ExecutorService service = Executors.newFixedThreadPool(5);

        final List<WorkItem> items = List.of(new WorkItem(), new WorkItem());

        final CountDownLatch finished = new CountDownLatch(items.size());
        for (WorkItem item : items)
        {
            service.submit(() ->
            {
                try
                {
                    synchronized (item)
                    {
                        // mutate object
                        item.field1 = "test";
                    }
                }
                finally
                {
                    finished.countDown();
                }
            });
        }
        finished.await();

        for (WorkItem item : items)
        {
            // will this make sure all changes done inside
            // threadpool worker threads are visible here ?
            synchronized (item)
            {
                // do stuff with work item
                System.out.println(item.field1);
            }
        }
    }
}

如果对共享对象的所有访问(读和写)都由所有线程在同步块中完成,那么访问是安全的。同步块添加了一个内存屏障,因此任何等待写入主内存的修改都会被提交。

Given the code below, will accessing fields of the WorkItem items in the main thread properly reflect any changes done to them by the threadpool worker threads ?

答案是肯定的,你会的。确保可见性有两个原因。

  1. synchronized(item):假设您没有 CountDownLatch(接下来会详细介绍),但您只有 synchronized(item)。由于您正在同步同一实例的读取和写入,因此读取线程将看到写入之前发生的更改。 但是,如果没有发生写入,那么您将看不到 test 的值,您只会看到 null。因此,即使它保证了内存可见性,也不能保证程序顺序。

  2. finished.await(); 现在假设您删除了 synchronized(item)。如果您只有 finished.countDown()finished.await(),您可以保证在 await 继续之前发生的任何更新现在在退出 await 后可见。因此,只需使用 CountDownLatch 即可为程序提供内存可见性和并发程序顺序。

来自 CountDownLatch javadocs

Memory consistency effects: Until the count reaches zero, actions in a thread prior to calling countDown() happen-before actions following a successful return from a corresponding await() in another thread.