guava的回调方案

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)

 */

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值