翻譯公司線程池實例:利用Executors和ThreadPoolExecutor
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從輸出結(jié)果看,線程池中有五個名為“pool-2-thread-2”‰“pool-2-thread-6”的任務(wù)線程擔(dān)任執(zhí)行提交的義務(wù)。
MyMonitorThread.java
WorkerPool.java
譯文鏈接:線程池?fù)?dān)任治理任務(wù)線程,英語翻譯,蘊(yùn)含一個期待執(zhí)行的義務(wù)隊列。線程池的義務(wù)隊列是一個Runnable匯合,任務(wù)線程擔(dān)任從義務(wù)隊列中取出并執(zhí)行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 提供了一些方法,可能查看執(zhí)行形狀、線程池大小、流動線程數(shù)和義務(wù)數(shù)。所以,我經(jīng)過一個監(jiān)眼簾程在固定間隔輸出執(zhí)行信息。
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請留意生動線程、已實現(xiàn)線程和義求實現(xiàn)總數(shù)的變化。咱們可能調(diào)用 shutdown() 完結(jié)一切已提交義務(wù)并終止線程池。
程序執(zhí)行的結(jié)果如下,確認(rèn)了上面的論斷:
WorkerThread.java
Executors?類利用 ExecutorService? 提供了一個 ThreadPoolExecutor 的簡略完成,但 ThreadPoolExecutor 提供的性能遠(yuǎn)不止這些。咱們可能指定創(chuàng)建 ThreadPoolExecutor 實例時生動的線程數(shù),并且可能限度線程池的大小,還可能創(chuàng)建本人的 RejectedExecutionHandler 完成來解決不合適放在任務(wù)隊列里的義務(wù)。
SimpleThreadPool.java
以上程序的輸出結(jié)果如下:
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 框架中創(chuàng)建固定大小的線程池:
關(guān)于作者: 彭秦進(jìn)
查看彭秦進(jìn)的更多文章 >>
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(); } }