1、ThreadPoolExecutor
1)线程池状态
ThreadPoolExecutor使用int的高3位来表示线程池状态,低29位表示线程数量
1.线程池的五种状态,只能由小到大迁移,即-1>0>1>2>3。
2.shutdown(不清空任务队列、 会等它们完成,shutdownNow)会清 空任务队列、不等它们完成。shutdown()只中断空闲的线程,shutdownNow()会 中断所有的线程,不管你执行没执行完。
3.TIDYING 和TREMINATED二者之间执行了一个钩子函数terminated(,目前这是-个空的实现。
为了保证线程状态和线程数量赋值操作的原子性,所以将两者存储在同一个原理变量中,用一次cas操作进行赋值
2)构造方法
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
- corePoolSize 核心线程数目(最多保留的线程数)
- maximumPoolSize 最大线程数目
- keepAliveTime 生存时间-针对救急线程
- unit 时间单位-针对救急线程
- workQueue 阻塞队列
- threadFactory 线程工厂- 可以为线程创建时起个好名字
- handler 拒绝策略
工作方式
核心线程和救急线程都是懒惰式创建,节约资源,使用的时候才创建
当核心线程都在执行任务,会将新加入的任务梵高阻塞队列中,当阻塞队列也满了,才会使用救急线程
-
救急线程和核心线程的区别就是,救济线程有生存时间,当任务执行完之后,在生存时间之内没有任务再执行,救急线程就会被销毁。
-
当救急线程也不够用于执行任务时,才会使用拒绝策略。当队列选择的是有界队列时,那么任务超过了队列大小才会创建救急线程
-
如果线程到达maximumPoolSize仍然有新任务这时会执行拒绝策略。拒绝策略jdk提供了4种实现,其它著名框架也提供了实现
-
AbortPolicy 让调用者抛出RejectedExecutionException异常,这是默认策略
-
CallerRunsPolicy 让调用者运行任务
-
DiscardPolicy 放弃本次任务
-
DiscardOldestPolicy 放弃队列中最早的任务,本任务取而代之
-
Dubbo 的实现,在抛出RejectedExecutionException异常之前会记录日志,并dump线程栈信息,方便定位问题
-
Netty 的实现,是创建一个新线程来执行任务
-
ActiveMQ的实现,带超时等待(60s) 尝试放入队列,类似我们之前自定义的拒绝策略
-
PinPoint 的实现,它使用了一个拒绝策略链,会逐一尝试策略链中每种拒绝策略
当高峰过去后,超过corePoolSize 的救急线程如果- -段时间没有任务做,需要结束节省资源,这个时间由keepAliveTime和unit来控制。
-
Executors
1)newFixedThreadPool
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
特点
-
核心线程数最大线程数(没有救急线程被创建),因此也无需超时时间
-
阻塞队列是无界的,可以放任意数量的任务
评价
适用于任务量已知,相对耗时的任务
代码:
public static void main(String[] args) {
ExecutorService pool= Executors.newFixedThreadPool(2);
pool.execute(()->{
System.out.println(Thread.currentThread()+"1");
});
pool.execute(()->{
System.out.println(Thread.currentThread()+"2");
});
pool.execute(()->{
System.out.println(Thread.currentThread()+"3");
});
}
不会随着主线程的结束而结束
自定义线程工厂–用于命名线程名字
public static void main(String[] args) {
ExecutorService pool= Executors.newFixedThreadPool(2, new ThreadFactory() {
private AtomicInteger t=new AtomicInteger(1);
@Override
public Thread newThread(Runnable r) {
return new Thread(r,"mypool_t"+t.getAndIncrement());
}
});
pool.execute(()->{
System.out.println(Thread.currentThread()+"1");
});
pool.execute(()->{
System.out.println(Thread.currentThread()+"2");
});
pool.execute(()->{
System.out.println(Thread.currentThread()+"3");
});
}
2)newCacheThreadPool
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
特点
-
核心线程数是0,最大线程数是Integer.MAX_ VALUE,救急线程的空闲生存时间是60s, 意味着
- 全部都是救急线程(60s 后可以回收)
- 救急线程可以无限创建
-
队列采用了SynchronousQueue 实现特点是,它没有容量,没有线程来取是放不进去的(- 手交钱、- -手交货)
评价
整个线程池表现为线程数会根据任务量不断增长,没有上限,当任务执行完毕,空闲1分钟后释放线程。
适合任务数比较密集,但每个任务执行时间较短的情况
3)newSingleThreadExecutor
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
使用场景:
希望多个任务排队执行。线程数固定为1,任务数多于1时,会放入无界队列排队。任务执行完毕,这唯一的线程也不会被释放。
区别:
- 自己创建一个单线程串行执行任务,如果任务执行失败而终止那么没有任何补救措施,而线程池还会新建-个线程,保证池的正常工作
- Executors.newSingleThreadExecutor() 线程个数始终为1,不能修改
- FinalizableDelegatedExecutorService 应用的是装饰器模式,只对外暴露了ExecutorService 接口,因此不能调用ThreadPoolExecutor中特有的方法
- Executors.newFixedThreadPool(1)初始时为1,以后还可以修改
- 对外暴露的是ThreadPoolExecutor对象,可以强转后调用setCorePoolSize 等方法进行修改
提交任务
Callable比Runnable多了返回值,Runnable也不能抛出异常
代码1:
package demo1;
import java.util.concurrent.*;
public class TestSumbit {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService pool = Executors.newFixedThreadPool(2);
Future<String> future=pool.submit(new Callable<String>() {
@Override
public String call() throws Exception {
System.out.println("running");
Thread.sleep(1000);
return "ok";
}
});
System.out.println(future.get());
}
}
代码2:
package demo1;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;
public class TestSumbit {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService pool = Executors.newFixedThreadPool(2);
/*Future<String> future=pool.submit(new Callable<String>() {
@Override
public String call() throws Exception {
System.out.println("running");
Thread.sleep(1000);
return "ok";
}
});*/
List<Future<Object>> futures = pool.invokeAll(Arrays.asList(
() ->{
System.out.println(Thread.currentThread() + "begin");
Thread.sleep(1000);
return "1";
},
() ->{
System.out.println(Thread.currentThread() + "begin");
Thread.sleep(500);
return "2";
},
() ->{
System.out.println(Thread.currentThread() + "begin");
Thread.sleep(2000);
return "3";
}
));
futures.forEach(f->{
try {
System.out.println(Thread.currentThread()+" "+f.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
});
//System.out.println(future.get());
}
}
关闭线程
shutdown
线程池状态变为SHUTDOWN
1、不会接收新任务
2、但已提交任务会执行完
3、此方法不会阻塞调用线程的执行
shutdownNow
线程池状态变为STOP
1、不会接收新任务
2、会将队列中的任务返回
3、并用interrupt 的方式中断正在执行的任务
List shutdownNow( );
任务调度线程池
在「任务调度线程池」功能加入之前,可以使用java.util.Timer来实现定时功能,Timer 的优点在于简单易
用,但由于所有任务都是由同一个线程来调度,因此所有任务都是串行执行的,同一时间只能有一个任务在执行,前一个任务的延迟或异常都将会影响到之后的任务。
代码:
public static void main(String[] args) {
ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
pool.schedule(()->{
System.out.println(Thread.currentThread()+"task1");
int i=1/0;
},1, TimeUnit.SECONDS);
pool.schedule(()->{
System.out.println(Thread.currentThread()+"task2");
},1, TimeUnit.SECONDS);
}
线程池中只有一个线程,所以线程只能串行运行,但是第一个线程代码出错,并不影响第二个线程的运行
定时执行
pool.scheduleAtFixedRate(): //上个线程开始到下个线程开始延时多长时间(如果线程执行时间大于延时时间,则按照线程执行结束后,立马开始下一个任务)
pool.scheduleWithFixedDelay()://从上一个线程结束延时指定时间后在执行下一个任务
处理线程池异常
1、任务自己用try块捉住
2、使用submit提交的返回值Future来判断(程序正常执行则返回正常结果,中间有异常则打印异常)
Future<Boolean> f = pool.submit(() -> {
System.out.println("tsak");
int i = 1 / 0;
return true;
});
System.out.println(f.get());
定时执行任务
如何让每周四18:00 定时执行任务
(任务,当前时间和任务执行时间的时间差,间隔时间,时间单位)
public static void main(String[] args) throws Exception {
//让每周周四18:00 定时执行任务
LocalDateTime now=LocalDateTime.now();//线程安全的一个时间类
//System.out.println(now);
//获得周四时间
LocalDateTime time = now.withHour(18).withMinute(0).withSecond(0).withNano(0).with(DayOfWeek.THURSDAY);
//如果当前时间> 本周周四,必须找到下周周四
if(now.compareTo(time)>0){
time=time.plusWeeks(1);//加一周时间
}
//计算当前时间和要执行任务时间的差值
long initailDelay = Duration.between(now, time).toMillis();
long period=1000*60*60*24*7;//一周的时间间隔
ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
pool.scheduleAtFixedRate(()->{
System.out.println("running");
},initailDelay,period,TimeUnit.MICROSECONDS);
}
time=time.plusWeeks(1);//加一周时间
}
//计算当前时间和要执行任务时间的差值
long initailDelay = Duration.between(now, time).toMillis();
long period=1000*60*60*24*7;//一周的时间间隔
ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
pool.scheduleAtFixedRate(()->{
System.out.println("running");
},initailDelay,period,TimeUnit.MICROSECONDS);
}