将值添加到来自不同线程的全局集中,并等待所有线程完成

时间:2018-04-20 07:51:34

标签: java multithreading set threadpool executorservice

我不熟悉线程,想知道我是否可以使用线程将大量数据分成小任务,从而减少处理时间 我已将Set拆分为多个集合列表,并且来自执行程序服务的每个线程都会占用该集合并将该集合添加到另一个集合(全局声明的) abcSet 。 我需要每个线程将对象添加到此集合中,并且在所有线程完成添加之后,继续执行必须使用 abcSet 完成的其余工作 下面是示例代码。请帮助!!

private static final int PARTITIONS_COUNT = 4;
final Set<Abc> newAbcSet = new HashSet<Abc>();
final Set<Abc> abcSet = //data from database
        ExecutorService e = Executors.newFixedThreadPool(4);
List<Set<Abc>> theSets = new ArrayList<Set<Abc>>(PARTITIONS_COUNT);
// divide set into 4 different sets for threading purpose
for (int i = 0; i < PARTITIONS_COUNT; i++) {
    theSets.add(new HashSet<Abc>());
}

int index = 0;
for (Abc abcObj : abcSet) {
    theSets.get(index++ % PARTITIONS_COUNT).add(abcObj);
}
for (final Set<Abc> abcSet1 : theSets) {
    e.execute(new Runnable() {
        @Override
        public void run() {
            for (Abc abc : abcSet1) {
                //do some modifications with abc and add it to newAbcSet
                newAbcSet.add(abc);
            }

        }
    });

}
//Do something with the newAbcSet

2 个答案:

答案 0 :(得分:1)

for添加while (!e.isTerminated()){}后停止执行:

for (final Set<Abc> abcSet1 : theSets) {
    e.execute(new Runnable() {
        @Override
        public void run() {
            for(Abc abc: abcSet1){
                //do some modifications with abc and add it to newAbcSet
                newAbcSet.add(abc);
            }

        }
    });
}

while (!e.isTerminated()){}

答案 1 :(得分:1)

  • 为了防止种族混淆,您应该使用thread safe Set implemention,例如:

    final Set<Abc> newAbcSet= Collections.synchronizedSet(new HashSet<Abc>());
    
  • 如果您不再需要线程池,可以拨打shutdown()awaitTermination()

    e.shutDown();  //  previously submitted tasks are not affected
    e.awaitTermination();  // Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.
    
    //Do something with the newAbcSet
    

    如果仍然需要线程池,请参阅此question