java - 如何在Java中的所有线程中维护List的单个副本

标签 java multithreading

class A implements Callable{

 List result     // want to share this List amongst thread and their function call

 public Search  call() {

 List<Job>  Jobs = api. Jobs(a,b,c);  // Multiple threads 

    }
 }


class API{
         public void jobs(a,b,c){
              // want to access the  A.Result and populate the result
         }
}     

我如何在所有线程中共享一个数组列表,我不想使用静态, 因为它每次运行时都会不断累积结果, Thread Local 是一个不错的选择吗?

试图避免额外的对象及其各自的 getter/setter?

最佳答案

你现在拥有的是线程共享列表。操作该对象的所有线程 (假设该对象只存在一个实例)除非同步,否则共享相同的列表。

关于java - 如何在Java中的所有线程中维护List的单个副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11894731/

相关文章:

android - 是否可以启动一个新的 Intent 并让它在单独的线程中运行?

c++ - Boost::Thread 或 fork() :多线程 HTTP 代理

Java Memcached 客户端

java - 用Java创建Excel文件

java - ServletActionContext.getRequest().removeAttribute() 不起作用

java - BlockPoolSliceScanner池不会停止

java - 线程和同步方法

c++ - 在 C++ 中具有明确定义的分配器的危险指针

c - 如何优化所需的线程数

java - 最新的 Android Studio 3.6.3 在 Windows 10 中安装后无法运行