文章內(nèi)容

翻譯公司線程池實(shí)例:利用Executors和ThreadPoolExecutor

時(shí)間:2018-05-20 13:28來源:網(wǎng)絡(luò)整理 作者:珠海翻譯公司 點(diǎn)擊:

RejectedExecutionHandlerImpl.java

下面是一個(gè) 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é)果看,線程池中有五個(gè)名為“pool-2-thread-2”‰“pool-2-thread-6”的任務(wù)線程擔(dān)任執(zhí)行提交的義務(wù)。

MyMonitorThread.java

WorkerPool.java

譯文鏈接:

線程池?fù)?dān)任治理任務(wù)線程,英語翻譯,蘊(yùn)含一個(gè)期待執(zhí)行的義務(wù)隊(duì)列。線程池的義務(wù)隊(duì)列是一個(gè)Runnable匯合,任務(wù)線程擔(dān)任從義務(wù)隊(duì)列中取出并執(zhí)行Runnable對(duì)象。


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í)行形狀、線程池大小、流動(dòng)線程數(shù)和義務(wù)數(shù)。所以,我經(jīng)過一個(gè)監(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

請(qǐng)留意生動(dòng)線程、已實(shí)現(xiàn)線程和義求實(shí)現(xiàn)總數(shù)的變化。咱們可能調(diào)用 shutdown() 完結(jié)一切已提交義務(wù)并終止線程池。

程序執(zhí)行的結(jié)果如下,確認(rèn)了上面的論斷:

WorkerThread.java

Executors?類利用 ExecutorService? 提供了一個(gè) ThreadPoolExecutor 的簡略完成,但 ThreadPoolExecutor 提供的性能遠(yuǎn)不止這些。咱們可能指定創(chuàng)建 ThreadPoolExecutor 實(shí)例時(shí)生動(dòng)的線程數(shù),并且可能限度線程池的大小,還可能創(chuàng)建本人的 RejectedExecutionHandler 完成來解決不合適放在任務(wù)隊(duì)列里的義務(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; } }

下面是一個(gè)測(cè)試程序,從 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(); } }