package org.example.test.cb;
import com.google.common.util.concurrent.*;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
@Slf4j
public class Main {
private static final ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
private static final ExecutorService logicThread = Executors.newSingleThreadExecutor(r -> {
Thread thread = new Thread(r);
thread.setName("logic-thread");
return thread;
});
public static void main(String[] args) {
AtomicInteger index = new AtomicInteger();
for (int i = 0; i < 5; i++) {
int threadIndex = index.incrementAndGet();
ListenableFuture<Integer> future = service.submit(() -> {
log.info("thread{} io线程 ", threadIndex);
if (threadIndex % 2 == 0) {
int a = 1 / 0; // 还就算出了异常,也会被guava捕获,不在onFailure中打印错误的话,就被吃掉了
}
return threadIndex;
});
Futures.addCallback(future, new FutureCallback<Integer>() {
@Override
public void onSuccess(Integer integer) {
log.info("thread{} 结果:{}", threadIndex, integer);
}
@Override
public void onFailure(Throwable throwable) {
log.error("error=", throwable); //
}
}, logicThread); // 回到指定的线程
}
}
}
/*
10:46:41.084 [pool-1-thread-5] INFO org.example.test.cb.Main - thread5 io线程
10:46:41.084 [pool-1-thread-4] INFO org.example.test.cb.Main - thread4 io线程
10:46:41.084 [pool-1-thread-3] INFO org.example.test.cb.Main - thread3 io线程
10:46:41.084 [pool-1-thread-2] INFO org.example.test.cb.Main - thread2 io线程
10:46:41.082 [pool-1-thread-1] INFO org.example.test.cb.Main - thread1 io线程
10:46:41.089 [logic-thread] INFO org.example.test.cb.Main - thread5 结果:5
10:46:41.090 [logic-thread] INFO org.example.test.cb.Main - thread1 结果:1
10:46:41.090 [logic-thread] INFO org.example.test.cb.Main - thread3 结果:3
10:46:41.092 [logic-thread] ERROR org.example.test.cb.Main - error=
java.lang.ArithmeticException: / by zero
at org.example.test.cb.Main.lambda$main$1(Main.java:29)
at com.google.common.util.concurrent.TrustedListenableFutureTask$TrustedFutureInterruptibleTask.runInterruptibly(TrustedListenableFutureTask.java:111)
at com.google.common.util.concurrent.InterruptibleTask.run(InterruptibleTask.java:58)
at com.google.common.util.concurrent.TrustedListenableFutureTask.run(TrustedListenableFutureTask.java:75)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
10:46:41.092 [logic-thread] ERROR org.example.test.cb.Main - error=
java.lang.ArithmeticException: / by zero
at org.example.test.cb.Main.lambda$main$1(Main.java:29)
at com.google.common.util.concurrent.TrustedListenableFutureTask$TrustedFutureInterruptibleTask.runInterruptibly(TrustedListenableFutureTask.java:111)
at com.google.common.util.concurrent.InterruptibleTask.run(InterruptibleTask.java:58)
at com.google.common.util.concurrent.TrustedListenableFutureTask.run(TrustedListenableFutureTask.java:75)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
*/
guava的回调方案
于 2024-03-06 10:37:11 首次发布