标签:new t 线程 min public 地方 底层工作原理 size 开发 rtp
1-ThreadPool线程池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<10;i++) {
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName()+"办理业务");
});
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
threadPool.shutdown();
}
}
一个都不用
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<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {*/
ExecutorService threadPool= new ThreadPoolExecutor(
2,
3,
2L,
TimeUnit.SECONDS,
new LinkedBlockingQueue(3),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.DiscardOldestPolicy());
try {
for (int i = 0; i < 10; i++) {
threadPool.execute(() -> {
System.out.println(Thread.currentThread().getName() + "办理业务");
});
}
} catch (Exception e) {
e.printStackTrace();
}finally{
threadPool.shutdown();
}
}
}
标签:new t 线程 min public 地方 底层工作原理 size 开发 rtp
原文地址:https://blog.51cto.com/14234228/2506980