几种常见的线程池
- Executors.newFixedThreadPool:固定线程大小的线程池,队列为LinkedBlockingQueue。
- Executors.newSingleThreadExecutor:单个线程的线程池,队列为LinkedBlockingQueue。
- Executors.newCachedThreadPool:无限大小的线程池,队列为SynchronousQueue,只能有一个元素。
- Executors.newScheduledThreadPool:无限大小的线程池,队列为DelayedWorkQueue。
代码
package com.mine.juc.threadpool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ExecutorsDemo {
public static void main(String[] args) throws Exception {
System.out.println("========演示newFixedThreadPool");
ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(3);
for (int i = 0; i < 10; i++) {
final String item = String.valueOf(i);
newFixedThreadPool.execute(new Thread(new Runnable() {
public void run() {
System.out.println(Thread.currentThread().getName() + ":" + item);
}
}));
}
newFixedThreadPool.shutdown();
Thread.sleep(1000);
System.out.println("========演示newFixedThreadPool");
System.out.println("========演示newSingleThreadExecutor");
ExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor();
for (int i = 0; i < 10; i++) {
final String item = String.valueOf(i);
newSingleThreadExecutor.execute(new Thread(new Runnable() {
public void run() {
System.out.println(Thread.currentThread().getName() + ":" + item);
}
}));
}
newSingleThreadExecutor.shutdown();
Thread.sleep(1000);
System.out.println("========演示newSingleThreadExecutor");
System.out.println("========演示newCachedThreadPool");
ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
final String item = String.valueOf(i);
newCachedThreadPool.execute(new Thread(new Runnable() {
public void run() {
System.out.println(Thread.currentThread().getName() + ":" + item);
}
}));
}
newCachedThreadPool.shutdown();
Thread.sleep(1000);
System.out.println("========演示newCachedThreadPool");
System.out.println("========演示newScheduledThreadPool");
ExecutorService newScheduledThreadPool = Executors.newScheduledThreadPool(2);
for (int i = 0; i < 10; i++) {
final String item = String.valueOf(i);
newScheduledThreadPool.execute(new Thread(new Runnable() {
public void run() {
System.out.println(Thread.currentThread().getName() + ":" + item);
}
}));
}
newScheduledThreadPool.shutdown();
Thread.sleep(1000);
System.out.println("========演示newScheduledThreadPool");
}
}
ThreadPoolExecutor七大参数
ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
- corePoolSize:核心线程数,一般情况下,即使空闲了,也不会回收。
- maximumPoolSize:最大线程数。
- keepAliveTime:存活时间。
- unit:存活时间的单位。
- workQueue:等待队列。
- threadFactory:线程工厂。
- handler:拒绝策略,有四种自带拒绝策略:AbortPolicy(默认策略,拒绝抛出异常)、CallerRunsPolicy(拒绝以后,返回执行线程的run方法)、DiscardOldestPolicy(丢弃等待最久的线程,然后把当前线程放入等待)、DiscardPolicy(直接丢弃当前线程)。
线程池工作流程
线程池处理流程:
- 当线程进来以后,如果核心线程池没有满,直接启动此线程。
- 如果核心线程已满,但是队列并没有满,把新来的线程加入工作队列。
- 如果添加到工作队列失败(也就是工作队列已满),尝试开启新的线程,如果成功的话,直接执行;如果开启新的线程失败,调用相应的拒绝策略。
代码
package com.mine.juc.threadpool;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadPoolExecutorDemo {
public static void main(String[] args) {
// 创建自定义线程池
// 核心线程数:3
// 最大线程数:5
// 空闲时间:1秒
// 工作队列:3
// 使用默认线程工厂
// 拒绝策略:抛出异常
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 5, 1, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(3), Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
// 模拟10个线程要执行,100毫秒来一个。每个线程执行需要1秒。
for (int i = 1; i <= 10; i++) {
final String item = String.valueOf(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
threadPoolExecutor.execute(new Runnable() {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":" + item);
}
});
} catch (Exception e) {
System.out.println("第" + item + "个被拒绝");
}
}
threadPoolExecutor.shutdown();
}
}
执行结果如下:线程执行顺序为1-2-3-7-8-4-5-6,其中7、8线程是在核心线程被占用,并且等待队列已满的情况下,线程池新增加了2个非核心线程执行的。
第9个被拒绝
第10个被拒绝
pool-1-thread-1:1
pool-1-thread-2:2
pool-1-thread-3:3
pool-1-thread-4:7
pool-1-thread-5:8
pool-1-thread-1:4
pool-1-thread-2:5
pool-1-thread-3:6