2019-08-07 工作中遇到的问题
事件情况描述: 有个线程池满了,然后新的任务使用CompletableFuture.supplyAsync
执行,用future.get(1, TimeUnit.SECONDS))
去获取(即使把get事件设置的很大)时报错java.util.concurrent.TimeoutException
报错java.util.concurrent.TimeoutException
觉得很奇怪;随后debug发现CompletableFuture.supplyAsync
的执行任务压根儿没执行;最后是调整了线程池解决
线程池满了,线程池被耗完了: 1,同时运行当任务太多,确实没有多的了;2,有耗时的任务,即使用个异步任务,也无法执行
- 复现代码 1
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @Author mubi
* @Date 2019/8/5 14:27
*/
public class Test{
static class MyThreadFactory implements ThreadFactory {
private AtomicInteger count = new AtomicInteger(0);
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
String threadName = "MyThread" + count.addAndGet(1);
// System.out.println(threadName);
t.setName(threadName);
return t;
}
}
static String printHello(){
return "hello";
}
public static void main(String[] args) {
int corePoolSize = 1;
int maximumPoolSize = 2;
int keepAliveTime = 60 * 1;
TimeUnit timeUnit = TimeUnit.SECONDS;
BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(2);
RejectedExecutionHandler handler = new ThreadPoolExecutor.DiscardPolicy();
// RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
ThreadFactory threadFactory = new MyThreadFactory();
// 线程池构造
ThreadPoolExecutor taskExecutor = new ThreadPoolExecutor(
corePoolSize,
maximumPoolSize,
keepAliveTime,
timeUnit,
workQueue,
threadFactory,
handler);
for (int i = 0; i < 10; i++) {
new Thread(() -> {
// 耗时操作,会沾满线程池
// taskExecutor.execute(() -> {
// try {
// TimeUnit.SECONDS.sleep(10);
// }catch (Exception e){
// e.printStackTrace();
// }
// });
// 第二步不耗时的操作,但是get的时候会报TimeoutException
CompletableFuture<Object> future1 = CompletableFuture.supplyAsync(Test::printHello, taskExecutor);
try {
System.out.println(Thread.currentThread().getName() + "::value1 " + future1.get(1, TimeUnit.SECONDS));
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
try {
TimeUnit.SECONDS.sleep(5000);
}catch (Exception e){
e.printStackTrace();
}
}
}
某次输出如下
Thread-9::value1 hello
Thread-3::value1 hello
Thread-2::value1 hello
Thread-8::value1 hello
java.util.concurrent.TimeoutException
at java.util.concurrent.CompletableFuture.timedGet(CompletableFuture.java:1771)
at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1915)
at Main.lambda$main$1(Main.java:539)
at java.lang.Thread.run(Thread.java:748)
java.util.concurrent.TimeoutException
at java.util.concurrent.CompletableFuture.timedGet(CompletableFuture.java:1771)
at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1915)
at Main.lambda$main$1(Main.java:539)
at java.lang.Thread.run(Thread.java:748)
java.util.concurrent.TimeoutException
at java.util.concurrent.CompletableFuture.timedGet(CompletableFuture.java:1771)
at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1915)
at Main.lambda$main$1(Main.java:539)
at java.lang.Thread.run(Thread.java:748)
java.util.concurrent.TimeoutException
at java.util.concurrent.CompletableFuture.timedGet(CompletableFuture.java:1771)
at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1915)
at Main.lambda$main$1(Main.java:539)
at java.lang.Thread.run(Thread.java:748)
java.util.concurrent.TimeoutException
at java.util.concurrent.CompletableFuture.timedGet(CompletableFuture.java:1771)
at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1915)
at Main.lambda$main$1(Main.java:539)
at java.lang.Thread.run(Thread.java:748)
java.util.concurrent.TimeoutException
at java.util.concurrent.CompletableFuture.timedGet(CompletableFuture.java:1771)
at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1915)
at Main.lambda$main$1(Main.java:539)
at java.lang.Thread.run(Thread.java:748)
- 复现代码 2
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @Author mubi
* @Date 2019/8/5 14:27
*/
public class Test{
static class MyThreadFactory implements ThreadFactory {
private AtomicInteger count = new AtomicInteger(0);
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
String threadName = "MyThread" + count.addAndGet(1);
System.out.println(threadName);
t.setName(threadName);
return t;
}
}
static String printHello(){
return "hello";
}
public static void main(String[] args) {
int corePoolSize = 1;
int maximumPoolSize = 2;
int keepAliveTime = 60 * 1;
TimeUnit timeUnit = TimeUnit.SECONDS;
BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(2);
RejectedExecutionHandler handler = new ThreadPoolExecutor.DiscardPolicy();
// RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
ThreadFactory threadFactory = new MyThreadFactory();
// 线程池构造
ThreadPoolExecutor taskExecutor = new ThreadPoolExecutor(
corePoolSize,
maximumPoolSize,
keepAliveTime,
timeUnit,
workQueue,
threadFactory,
handler);
for (int i = 1; i < 3; i++) {
new Thread(() -> {
// 耗时操作,会沾满线程池
taskExecutor.execute(() -> {
try {
TimeUnit.SECONDS.sleep(10);
}catch (Exception e){
e.printStackTrace();
}
});
// 第二步不耗时的操作,但是get的时候会报TimeoutException
CompletableFuture<Object> future1 = CompletableFuture.supplyAsync(Test::printHello, taskExecutor);
try {
System.out.println(Thread.currentThread().getName() + "::value1 " + future1.get(1, TimeUnit.SECONDS));
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
try {
TimeUnit.SECONDS.sleep(5000);
}catch (Exception e){
e.printStackTrace();
}
}
}
- 输出如下
Thread-0::value1 hello
java.util.concurrent.TimeoutException
at java.util.concurrent.CompletableFuture.timedGet(CompletableFuture.java:1771)
at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1915)
at Main.lambda$main$2(Main.java:539)
at java.lang.Thread.run(Thread.java:748)