扩展线程池ThreadPoolExecutor的简单例子

来源:互联网 发布:东方市网络诈骗2017 编辑:程序博客网 时间:2024/06/05 11:29
import java.util.concurrent.BlockingQueue;import java.util.concurrent.SynchronousQueue;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;//扩展线程池以提供日志和计时功能public class TimingThreadPool extends ThreadPoolExecutor{//需要重写配置型的构造方法public TimingThreadPool(int corePoolSize, int maximumPoolSize,long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);}public static void main(String[] args) {//默认使用Executors.newCachedThreadPool()的配置方法,过期时间为60秒TimingThreadPool pool = new TimingThreadPool(0, Integer.MAX_VALUE,                60L, TimeUnit.SECONDS,new SynchronousQueue<Runnable>());pool.runTask();pool.shutdown();}//执行任务public void runTask(){this.execute(new Runnable(){@Overridepublic void run() {System.out.println("我执行了一个任务~");}});}//执行任务之前@Overrideprotected void beforeExecute(Thread t, Runnable r) {super.beforeExecute(t, r);System.out.println("执行任务之前~");}//执行任务之后@Overrideprotected void afterExecute(Runnable r, Throwable t) {super.afterExecute(r, t);System.out.println("执行任务之后~");}//执行任务完成,需要执行关闭操作才会调用这个方法@Overrideprotected void terminated() {super.terminated();System.out.println("执行任务完成~");}/** * 运行结果: *  执行任务之前~我执行了一个任务~执行任务之后~执行任务完成~ */}

上面的是一个程序的简单描述,下面的程序会有实际意义点

import java.util.Random;import java.util.concurrent.BlockingQueue;import java.util.concurrent.SynchronousQueue;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicInteger;import java.util.concurrent.atomic.AtomicLong;public class LogThreadPool extends ThreadPoolExecutor{public LogThreadPool(int corePoolSize, int maximumPoolSize,long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);}private final AtomicInteger numTasks = new AtomicInteger();//线程个数private final AtomicLong totalTimes = new AtomicLong();//所有线程总执行时间private ThreadLocal<Long> startTime = new ThreadLocal<Long>();public static void main(String[] args) {ThreadPoolExecutor pool = new LogThreadPool(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());Runnable task = new Runnable(){@Overridepublic void run() {System.out.println(Thread.currentThread().getName()+"任务正在执行");try {int rand = new Random().nextInt(1000);Thread.sleep(rand);} catch (InterruptedException e) {e.printStackTrace();}}};int ThreadNum = 5;for(int i=0; i<ThreadNum; i++){pool.execute(task);}pool.shutdown();}//在执行给定线程中的给定 Runnable 之前调用的方法@Overrideprotected void beforeExecute(Thread t, Runnable r) {super.beforeExecute(t, r);System.out.println(Thread.currentThread().getName()+"任务执行之前..");startTime.set(System.currentTimeMillis());}//基于完成执行给定 Runnable 所调用的方法@Overrideprotected void afterExecute(Runnable r, Throwable t) {super.afterExecute(r, t);long endTime = System.currentTimeMillis();long tasktime = endTime - startTime.get();numTasks.incrementAndGet();System.out.println(Thread.currentThread().getName()+"任务执行之后运行毫秒数:"+tasktime);totalTimes.addAndGet(tasktime);}//当 Executor 已经终止时调用的方法@Overrideprotected void terminated() {super.terminated();System.out.println("任务全部完成.......");System.out.println("共有几个线程执行:"+numTasks);System.out.println("共运行了几毫秒:"+totalTimes.get());}/** * 运行结果: *  pool-1-thread-1任务执行之前..pool-1-thread-1任务正在执行pool-1-thread-2任务执行之前..pool-1-thread-2任务正在执行pool-1-thread-4任务执行之前..pool-1-thread-4任务正在执行pool-1-thread-3任务执行之前..pool-1-thread-3任务正在执行pool-1-thread-5任务执行之前..pool-1-thread-5任务正在执行pool-1-thread-1任务执行之后运行毫秒数:47pool-1-thread-3任务执行之后运行毫秒数:407pool-1-thread-4任务执行之后运行毫秒数:469pool-1-thread-2任务执行之后运行毫秒数:500pool-1-thread-5任务执行之后运行毫秒数:703任务全部完成.......共有几个线程执行:5共运行了几毫秒:2126 */}