文章内容

翻译公司线程池实例:利用Executors和ThreadPoolExecutor

时间:2018-05-20 13:28来源:网络整理 作者:珠海翻译公司 点击:

RejectedExecutionHandlerImpl.java

下面是一个 RejectedExecutionHandler 接口的自定义完成:

pool-2-thread-2 Start. Command = 2 pool-2-thread-4 Start. Command = 4 pool-2-thread-2 Start. Command = 0 pool-2-thread-4 Start. Command = 2 pool-2-thread-6 Start. Command = 4 pool-2-thread-4 End. pool-2-thread-6 End. pool-2-thread-2 End. pool-2-thread-4 End. pool-2-thread-4 Start. Command = 8 pool-2-thread-2 End. pool-2-thread-2 Start. Command = 0 pool-2-thread-2 Start. Command = 8 pool-2-thread-6 Start. Command = 6 pool-2-thread-4 Start. Command = 6 pool-2-thread-2 End. pool-2-thread-4 End. pool-2-thread-4 End. pool-2-thread-6 End. pool-2-thread-2 End. Finished all threads

从输出结果看,线程池中有五个名为“pool-2-thread-2”‰“pool-2-thread-6”的任务线程担任执行提交的义务。

MyMonitorThread.java

WorkerPool.java

译文链接:

线程池担任治理任务线程,英语翻译,蕴含一个期待执行的义务队列。线程池的义务队列是一个Runnable汇合,任务线程担任从义务队列中取出并执行Runnable对象。


package com.journaldev.threadpool; import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ThreadPoolExecutor; public class RejectedExecutionHandlerImpl implements RejectedExecutionHandler { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { System.out.println(r.toString() + " is rejected"); } }

ThreadPoolExecutor 提供了一些方法,可能查看执行形状、线程池大小、流动线程数和义务数。所以,我经过一个监眼帘程在固定间隔输出执行信息。

pool-2-thread-2 Start. Command = cmd0 pool-2-thread-4 Start. Command = cmd6 cmd6 is rejected pool-2-thread-4 Start. Command = cmd4 pool-2-thread-2 Start. Command = cmd2 cmd8 is rejected cmd8 is rejected cmd0 is rejected [monitor] [0/2] Active: 4, Completed: 0, Task: 6, isShutdown: false, isTerminated: false [monitor] [4/2] Active: 4, Completed: 0, Task: 6, isShutdown: false, isTerminated: false pool-2-thread-4 End. pool-2-thread-2 End. pool-2-thread-2 End. pool-2-thread-4 End. pool-2-thread-2 Start. Command = cmd4 pool-2-thread-4 Start. Command = cmd2 [monitor] [4/2] Active: 2, Completed: 4, Task: 6, isShutdown: false, isTerminated: false [monitor] [4/2] Active: 2, Completed: 4, Task: 6, isShutdown: false, isTerminated: false pool-2-thread-2 End. pool-2-thread-4 End. [monitor] [4/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false [monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false [monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false [monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false [monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false [monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false [monitor] [0/2] Active: 0, Completed: 6, Task: 6, isShutdown: true, isTerminated: true [monitor] [0/2] Active: 0, Completed: 6, Task: 6, isShutdown: true, isTerminated: true

请留意生动线程、已实现线程和义求实现总数的变化。咱们可能调用 shutdown() 完结一切已提交义务并终止线程池。

程序执行的结果如下,确认了上面的论断:

WorkerThread.java

Executors 类利用 ExecutorService  提供了一个 ThreadPoolExecutor 的简略完成,但 ThreadPoolExecutor 提供的性能远不止这些。咱们可能指定创建 ThreadPoolExecutor 实例时生动的线程数,并且可能限度线程池的大小,还可能创建本人的 RejectedExecutionHandler 完成来解决不合适放在任务队列里的义务。

SimpleThreadPool.java

以上程序的输出结果如下:

package com.journaldev.threadpool; import java.util.concurrent.ThreadPoolExecutor; public class MyMonitorThread implements Runnable { private ThreadPoolExecutor executor; private int seconds; private boolean run=true; public MyMonitorThread(ThreadPoolExecutor executor, int delay) { this.executor = executor; this.seconds=delay; } public void shutdown(){ this.run=false; } @Override public void run() { while(run){ System.out.println( String.format("[monitor] [%d/%d] Active: %d, Completed: %d, Task: %d, isShutdown: %s, isTerminated: %s", this.executor.getPoolSize(), this.executor.getCorePoolSize(), this.executor.getActiveCount(), this.executor.getCompletedTaskCount(), this.executor.getTaskCount(), this.executor.isShutdown(), this.executor.isTerminated())); try { Thread.sleep(seconds*2000); } catch (InterruptedException e) { e.printStackTrace(); } } } }

下面是利用 ThreadPoolExecutor 的线程池完成示例:

package com.journaldev.threadpool; public class WorkerThread implements Runnable { private String command; public WorkerThread(String s){ this字符串mand=s; } @Override public void run() { System.out.println(Thread.currentThread().getName()+" Start. Command = "+command); processCommand(); System.out.println(Thread.currentThread().getName()+" End."); } private void processCommand() { try { Thread.sleep(6000); } catch (InterruptedException e) { e.printStackTrace(); } } @Override public String toString(){ return this字符串mand; } }

下面是一个测试程序,从 Executors 框架中创建固定大小的线程池:

关于作者: 彭秦进

查看彭秦进的更多文章 >>

package com.journaldev.threadpool; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class WorkerPool { public static void main(String args[]) throws InterruptedException{ //RejectedExecutionHandler implementation RejectedExecutionHandlerImpl rejectionHandler = new RejectedExecutionHandlerImpl(); //Get the ThreadFactory implementation to use ThreadFactory threadFactory = Executors.defaultThreadFactory(); //creating the ThreadPoolExecutor ThreadPoolExecutor executorPool = new ThreadPoolExecutor(2, 4, 20, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2), threadFactory, rejectionHandler); //start the monitoring thread MyMonitorThread monitor = new MyMonitorThread(executorPool, 4); Thread monitorThread = new Thread(monitor); monitorThread.start(); //submit work to the thread pool for(int i=0; i<20; i++){ executorPool.execute(new WorkerThread("cmd"+i)); } Thread.sleep(40000); //shut down the pool executorPool.shutdown(); //shut down the monitor thread Thread.sleep(6000); monitor.shutdown(); } }