我们有一个很大的文本文件,其中每一行都需要大量的 process
.该设计将具有一个 class
,该类读取文件并通过 thread pool
将每一行的处理委托给 thread
.一旦池中没有空闲线程可以进行处理,则应阻止文件读取器类读取下一行.所以我需要一个阻塞线程池
We have a large text file in which each line requires intensive process
. The design is to have a class
that reads the file and delegates the processing of each line to a thread
, via thread pool
. The file reader class should be blocked from reading the next line once there is no free thread in the pool to do the processing. So i need a blocking thread pool
在当前实现中, ThreadPoolExecutor.submit()
和 ThreadPoolExecutor.execute()
方法在配置的线程数获取后抛出 RejectedExecutionException
异常如下面的代码片段所示,我很忙.
In the current implementation ThreadPoolExecutor.submit()
and ThreadPoolExecutor.execute()
methods throw RejectedExecutionException
exception after the configured # of threads get busy as i showed in code snippet below.
public class BlockingTp { public static void main(String[] args) { ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3); ThreadPoolExecutor executorService= new ThreadPoolExecutor(1, 3, 30, TimeUnit.SECONDS, arrayBlockingQueue); int Jobs = 10; System.out.println("Starting application with " + Jobs + " jobs"); for (int i = 1; i <= Jobs; i++) try { executorService.submit(new WorkerThread(i)); System.out.println("job added " + (i)); } catch (RejectedExecutionException e) { System.err.println("RejectedExecutionException"); } } } class WorkerThread implements Runnable { int job; public WorkerThread(int job) { this.job = job; } public void run() { try { Thread.sleep(1000); } catch (Exception excep) { } } }
以上程序的输出为
Starting application to add 10 jobs Added job #1 Added job #2 Added job #3 Added job #4 Added job #5 Added job #6 RejectedExecutionException RejectedExecutionException RejectedExecutionException RejectedExecutionException
有人可以说点什么,就是说我如何实现阻塞线程池.
Can some one throw some light i.e how i can implement blocking thread pool.
有人可以提供一些启示,即我如何实现阻塞线程池.
Can some one throw some light i.e how i can implement blocking thread pool.
您需要在执行程序服务上设置拒绝执行处理程序.
You need to set a rejection execution handler on your executor service.
BlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3); ThreadPoolExecutor executorService = new ThreadPoolExecutor(1, 3, 30, TimeUnit.SECONDS, arrayBlockingQueue); // when the blocking queue is full, this tries to put into the queue which blocks executorService.setRejectedExecutionHandler(new RejectedExecutionHandler() { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { try { // block until there's room executor.getQueue().put(r); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RejectedExecutionException("Producer thread interrupted", e); } } });
因此,它不会调用TRE抛出 RejectedExecutionException
,而是将调用拒绝处理程序,该处理程序将依次尝试将作业放回队列.这会阻止呼叫者.
So instead of the TRE throwing a RejectedExecutionException
, it will call the rejection handler which will in turn try to put the job back on the queue. This blocks the caller.
这篇关于如何实现阻塞线程池执行器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!