java线程池 ThreadPoolExecutor

ThreadPoolExecutor用法参照:

http://sjsky.iteye.com/blog/1100208 

 

参数详细说明:
[ 1 ]、corePoolSize: 线程池维护线程的最少数量
[ 2 ]、maximumPoolSize:线程池维护线程的最大数量
[ 3 ]、keepAliveTime: 线程池维护线程所允许的空闲时间
[ 4 ]、unit: 线程池维护线程所允许的空闲时间的单位,unit可选的参数为java.util.concurrent.TimeUnit中的几个静态属性:
NANOSECONDS
MICROSECONDS
MILLISECONDS
SECONDS
[ 5]、workQueue: 线程池所使用的缓冲队列,常用的是:java.util.concurrent.ArrayBlockingQueue
[ 6 ]、handler: 线程池对拒绝任务的处理策略,有四个选择如下:
ThreadPoolExecutor.AbortPolicy():抛出java.util.concurrent.RejectedExecutionException异常
ThreadPoolExecutor.CallerRunsPolicy():重试添加当前的任务,他会自动重复调用execute()方法
ThreadPoolExecutor.DiscardOldestPolicy():抛弃旧的任务
ThreadPoolExecutor.DiscardPolicy():抛弃当前的任务
 


如果我们试图给线程加入daemonpriority的属性设置,那么还需要ThreadFactory,用法如下:

import java.util.concurrent.ThreadFactory;
 
public class DaemonThreadFactory implements ThreadFactory {
    public Thread newThread(Runnable r) {
       Thread t = new Thread(r);
       t.setDaemon(true);
       return t;
    }
}

构造函数:

 

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler)

你可能感兴趣的:(java线程池 ThreadPoolExecutor)