目录
目录
目录
创建线程池
测试代码
运行线程
全量代码
日志
AbortPolicy 报出异常模式
DiscardPolicy 放弃机制啥也不处理
DiscardOldestPolicy 放弃机制,放弃列队最早进入的
CallerRunsPolicy 交给主线程执行
创建线程池
public static ExecutorService executor = new ThreadPoolExecutor(2,
2, 60L, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(3), Executors.defaultThreadFactory(),
// new ThreadPoolExecutor.AbortPolicy()
// new ThreadPoolExecutor.DiscardPolicy()
new ThreadPoolExecutor.DiscardOldestPolicy()
// new ThreadPoolExecutor.CallerRunsPolicy()
);
测试代码
@Test
public void t1() throws InterruptedException {
for (int i = 0; i < 9; i++) {
String finalI = i+"";
try {
ex(finalI);
}catch (RejectedExecutionException e){
System.out.println(finalI+" error = "+e);
Thread.sleep(2);
ex(finalI+" 重新执行");
}
}
Thread.sleep(1000*5);
}
运行线程
public static void ex(String i) throws RejectedExecutionException{
String finalI = i;
executor.execute(new Runnable() {
private String finalC = finalI;
@Override
public void run() {
// System.out.println(Thread.currentThread().getName()+" == start =="+ finalC);
try {
Thread.sleep(2);
} catch (InterruptedException e) {
System.out.println("error = "+e);
}
System.out.println(Thread.currentThread().getName()+" == success =="+ finalC);
}
});
}
全量代码
import org.junit.jupiter.api.Test;
import java.util.concurrent.*;
public class TeTh {
public static ExecutorService executor = new ThreadPoolExecutor(2,
2, 60L, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(3), Executors.defaultThreadFactory(),
// new ThreadPoolExecutor.AbortPolicy()
// new ThreadPoolExecutor.DiscardPolicy()
new ThreadPoolExecutor.DiscardOldestPolicy()
// new ThreadPoolExecutor.CallerRunsPolicy()
);
@Test
public void t1() throws InterruptedException {
for (int i = 0; i < 9; i++) {
String finalI = i+"";
try {
ex(finalI);
}catch (RejectedExecutionException e){
System.out.println(finalI+" error = "+e);
Thread.sleep(2);
ex(finalI+" 重新执行");
}
}
Thread.sleep(1000*5);
}
public static void ex(String i) throws RejectedExecutionException{
String finalI = i;
executor.execute(new Runnable() {
private String finalC = finalI;
@Override
public void run() {
// System.out.println(Thread.currentThread().getName()+" == start =="+ finalC);
try {
Thread.sleep(2);
} catch (InterruptedException e) {
System.out.println("error = "+e);
}
System.out.println(Thread.currentThread().getName()+" == success =="+ finalC);
}
});
}
}
日志
AbortPolicy 报出异常模式
5 error = java.util.concurrent.RejectedExecutionException: Task org.cc.automaton.security.TeTh$1@172ed45 rejected from java.util.concurrent.ThreadPoolExecutor@79c292[Running, pool size = 2, active threads = 2, queued tasks = 3, completed tasks = 0]
pool-1-thread-2 == success ==1
pool-1-thread-1 == success ==0
7 error = java.util.concurrent.RejectedExecutionException: Task org.cc.automaton.security.TeTh$1@1574691 rejected from java.util.concurrent.ThreadPoolExecutor@79c292[Running, pool size = 2, active threads = 2, queued tasks = 3, completed tasks = 2]
pool-1-thread-2 == success ==2
pool-1-thread-1 == success ==3
pool-1-thread-2 == success ==4
pool-1-thread-1 == success ==5 重新执行
pool-1-thread-2 == success ==6
pool-1-thread-1 == success ==7 重新执行
pool-1-thread-2 == success ==8
DiscardPolicy 放弃机制啥也不处理
pool-1-thread-1 == success ==0
pool-1-thread-2 == success ==1
pool-1-thread-1 == success ==2
pool-1-thread-2 == success ==3
pool-1-thread-1 == success ==4
DiscardOldestPolicy 放弃机制,放弃列队最早进入的
pool-1-thread-1 == success ==0
pool-1-thread-2 == success ==1
pool-1-thread-1 == success ==6
pool-1-thread-2 == success ==7
pool-1-thread-1 == success ==8
CallerRunsPolicy 交给主线程执行
pool-1-thread-2 == success ==1
pool-1-thread-1 == success ==0
main == success ==5
pool-1-thread-1 == success ==3
main == success ==8
pool-1-thread-2 == success ==2
pool-1-thread-1 == success ==4
pool-1-thread-2 == success ==6
pool-1-thread-1 == success ==7
ok
持续更新