简体   繁体   English

使用Threadpool和执行程序服务进行处理

[英]Processing with Threadpool and executor service

I have an ArrayList of 10 elements. 我有10个元素的ArrayList。 I initiate a threadpool of size 10 and call execute with an element passed into each thread. 我启动一个大小为10的线程池,并使用传递到每个线程中的元素调用execute。 Each thread does some processing with that element as input and outputs the result. 每个线程都以该元素作为输入进行一些处理,然后输出结果。 The problem is, the output sometimes has processing results of only 7 elements sometimes and 8 sometimes with a few duplicates and 9 sometimes. 问题是,输出有时仅具有7个元素的处理结果,有时有时有8个元素的处理结果,有时则有9个元素。 I am not sure why I do not have the processing results of exactly 10 elements. 我不确定为什么我没有恰好10个元素的处理结果。 Here's my code snippet. 这是我的代码段。

ExecutorService exeSvc = 
                Executors.newFixedThreadPool(10)
for (Object element: arlList)//arlList is the arraylist of 
                                                           size-10
{
   exeSvc.execute({->myRunnable element});
}

What am I doing wrong? 我究竟做错了什么?

groovy... groovy ...

import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit

ExecutorService exeSvc = Executors.newFixedThreadPool(5)
for (int element=0;element<9;element++) {
    int elementCopy = element
    exeSvc.execute({->
        Thread.sleep(567); 
        println "${Thread.currentThread()}  element = $element elementCopy = $elementCopy"; 
    });
}
println "All Started"
exeSvc.shutdown()
exeSvc.awaitTermination(10, TimeUnit.SECONDS)
println "All Finished"

outputs 输出

All Started
Thread[pool-12-thread-1,5,main]  element = 9 elementCopy = 0
Thread[pool-12-thread-2,5,main]  element = 9 elementCopy = 1
Thread[pool-12-thread-3,5,main]  element = 9 elementCopy = 2
Thread[pool-12-thread-4,5,main]  element = 9 elementCopy = 3
Thread[pool-12-thread-5,5,main]  element = 9 elementCopy = 4
Thread[pool-12-thread-1,5,main]  element = 9 elementCopy = 5
Thread[pool-12-thread-2,5,main]  element = 9 elementCopy = 6
Thread[pool-12-thread-3,5,main]  element = 9 elementCopy = 7
Thread[pool-12-thread-4,5,main]  element = 9 elementCopy = 8
All Finished

as you can see in my case the for loop finished before threads started and all of them has element value 9 and elementCopy is different 如您所见,在我的情况下, for循环在线程启动之前完成,并且所有循环的element值均为9, elementCopy不同

That's because main thread finishes erlier than ExecutorService . 那是因为main threadExecutorService完成时间要差。

To force Main thread to wait for ExecutorService to finish processing, use this: 要强制Main thread等待ExecutorService完成处理,请使用以下命令:

exeSvc.awaitTermination(5, TimeUnit.SECONDS); //this will 5 second to finish all tasks
exeSvc.shutdown();

Execution time for each element is not the same. 每个元素的执行时间不相同。 So, at the end of process, shutting down thread is required. 因此,在过程结束时,需要关闭线程。 You can use, 您可以使用,

if ( check_whether_operation_is_done ) {
    Logger.info( "Process shutdown" );
    exeSvc.shutdownNow();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM