线程池ThreadPoolExecutor
2021-05-05 01:31
标签:new t 线程 min public 地方 底层工作原理 size 开发 rtp 一个都不用 线程池ThreadPoolExecutor 标签:new t 线程 min public 地方 底层工作原理 size 开发 rtp 原文地址:https://blog.51cto.com/14234228/25069801.1 为什么要用线程池
1.2 线程池的架构--ThreadPoolExecutor
1.3 线程池的构造方法
package thread20200415;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* 线程池:ThreadPoolExecutor
* @author zhaomin
* @date 2020/4/15 23:39
*/
public class ThreadPoolTest {
public static void main(String[] args) {
//一池多线程
//ExecutorService threadPool= Executors.newFixedThreadPool(5);
//一池一线程
// ExecutorService threadPool= Executors.newSingleThreadExecutor();
//一池N线程
ExecutorService threadPool= Executors.newCachedThreadPool();
for(int i=0;i{
System.out.println(Thread.currentThread().getName()+"办理业务");
});
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
threadPool.shutdown();
}
}
1.4线程池中的几个重要参数
1.5线程池底层工作原理
9.6 ***的、单一的、可扩容的用哪一种?
1.7 拒绝策略
package thread20200415;
import java.util.concurrent.*;
/**
* 线程池:ThreadPoolExecutor
* @author zhaomin
* @date 2020/4/15 23:39
*/
public class ThreadPoolTest {
public static void main(String[] args) {
//一池多线程
//ExecutorService threadPool= Executors.newFixedThreadPool(5);
//一池一线程
// ExecutorService threadPool= Executors.newSingleThreadExecutor();
//一池N线程
// ExecutorService threadPool= Executors.newCachedThreadPool();
/*在实际开发中,要用自定义的线程池
* ThreadPoolExecutor.AbortPolicy()---会报异常
* ThreadPoolExecutor.CallerRunsPolicy()--多余的任务返回给调用者线程来执行
* ThreadPoolExecutor.DiscardPolicy()--直接拒绝,默默工作--最好了
* ThreadPoolExecutor.DiscardOldestPolicy()--踢掉最早来到阻塞队列等待的任务,给新任务腾地方*/
/**public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue
下一篇:java小例子