参数说明:
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
corePoolSize:指定了线程池中的数量。
maximumPoolSize:线程池中的最大线程数量。
keepAliveTime:当线程池超过corePoolSize数量的时候,多余的空闲线程的存活时间。即空闲线程在多长时间后销毁。
unit:时间单位。
workQueue:任务队列,被提交但尚未被执行的任务。
ThreadFactory:线程工厂,一般用改默认即可。
Handler:拒绝策略,当任务太多来不及处理,如何拒绝任务。
4种并发包下提供的线程池:
1.ExecutorService executorService = Executors.newSingleThreadExecutor();
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
2.ExecutorService executorService2 = Executors.newCachedThreadPool();
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
3.ScheduledThreadPoolExecutor executorService3 = (ScheduledThreadPoolExecutor) Executors.newFixedThreadPool(1);
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
4.ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1); 定时运行
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS,
new DelayedWorkQueue());
}
scheduleWithFixedDelay: 比如当前一个任务结束的时刻,开始结算间隔时间,如0秒开始执行第一次任务,任务耗时5秒,任务间隔时间3秒,那么第二次任务执行的时间是在第8秒开始。
scheduleAtFixedRate:没有什么歧义,很容易理解,就是每隔多少时间,固定执行任务。
scheduledExecutorService.scheduleAtFixedRate(new ThreadTest(), 1, 2, TimeUnit.SECONDS);
scheduledExecutorService.scheduleWithFixedDelay(new ThreadTest(), 1, 2, TimeUnit.SECONDS);
自定义线程池以及拒绝策略:
package 第三章.线程池;
import java.util.concurrent.*;
/**
* Created by zzq on 2018/2/5.
*/
public class 自定义线程池 {
public static void main(String[] args) {
ThreadPoolExecutor threadPoolExecutor=new ThreadPoolExecutor(2,
5,
0,
TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(2),
Executors.defaultThreadFactory(),
// new ThreadPoolExecutor.AbortPolicy());//第一种拒绝策略:直接抛出异常AbortPolicy
// new ThreadPoolExecutor.CallerRunsPolicy());//第二种拒绝策略:新建一个线程
// new ThreadPoolExecutor.DiscardOldestPolicy());//第三种拒绝策略:在队列里推出一个最早的
// new ThreadPoolExecutor.DiscardPolicy());//第四种:直接丢弃。
new RejectedExecutionHandler() {
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
System.out.println(Thread.currentThread().getName()+"被抛弃了");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"拒绝成功");
}
});//自定义拒绝策略
for (int i = 0; i < 10; i++) {
final int finalI = i;
threadPoolExecutor.execute(new Runnable() {
public void run() {
System.out.println(Thread.currentThread().getName()+"开始"+ finalI);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"结束"+ finalI); }
});
}
}
}
自定义线程创建:
package 第三章.线程池;
import java.util.concurrent.*;
/**
* Created by zzq on 2018/2/5.
*/
public class 自定义线程创建 {
public static void main(String[] args) {
ThreadPoolExecutor threadPoolExecutor=new ThreadPoolExecutor(2,
5,
0,
TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(2),
new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t=new Thread(r);
System.out.println("Creat");
return t;
}
},
// new ThreadPoolExecutor.AbortPolicy());//第一种拒绝策略:直接抛出异常AbortPolicy
new ThreadPoolExecutor.CallerRunsPolicy());//第二种拒绝策略:新建一个线程
// new ThreadPoolExecutor.DiscardOldestPolicy());//第三种拒绝策略:在队列里推出一个最早的
// new ThreadPoolExecutor.DiscardPolicy());//第四种:直接丢弃。
// new RejectedExecutionHandler() {
// public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
// System.out.println(Thread.currentThread().getName() + "被抛弃了");
// try {
// Thread.sleep(2000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// System.out.println(Thread.currentThread().getName() + "拒绝成功");
//
// }
// });//自定义拒绝策略。
for (int i = 0; i < 10; i++) {
final int finalI = i;
threadPoolExecutor.execute(new Runnable() {
public void run() {
System.out.println(Thread.currentThread().getName()+"开始"+ finalI);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"结束"+ finalI); }
});
}
}
}