这种多线程迭代是否安全?

Mar*_*ace 2 java multithreading iterator

我有一个集合,我想产生一些线程来对它的元素做一些繁重的工作.集合的每个元素必须只处理一次.我希望尽可能减少同步,我想出了以下代码:

//getting the iterator is actually more complicated in my specific case
final Iterator it = myCollection.terator(); 
Thread[] threads = new Thread[numThreads];

for( int i = 0; i < numThreads; i++ ) {
    threads[i] = new Thread(new Runnable() {
        public void run() {
            Object obj = null;
            while(true) {
                synchronized (it) {
                    if(it.hasNext())
                        obj = it.next();
                    else
                        return;
                }
                //Do stuff with obj
            }
        }
    });
    threads[i].start();
}

for (Thread t : threads)
    try {
        t.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

注意:没有线程会在"使用obj做东西"时添加或删除项目来修改集合

这段代码与我在周围人们倾向于在集合本身上同步,使用Collection.synchronizedStuff..或者只是在整个迭代中同步的示例完全不同.在我的研究过程中,我发现可能使用更好的替代方案,ThreadPoolExecutor但让我们暂时忘掉它...

考虑到上面的注1,上面的代码是否安全?如果没有,为什么?

Pet*_*rey 5

我根本不会使用同步.

我会有一个循环,它将任务添加到ExecutorService.

ExecutorService es = Executors.newFixedThreadPool(nThreads);

for(final MyType mt: myCollection)
    es.submit(new Runnable() {
       public void run() {
           doStuffWith(mt);
       }
    });
es.shutdown();
es.awaitTermination(1, TimeUnit.HOURS);
Run Code Online (Sandbox Code Playgroud)

如果删除创建和关闭线程池的需要,它甚至更短.