拥抱变化,Hello Halo!

因为 hexo / hugo 全文搜索体验较差,迁移到 Halo 体验一下。

相关链接

在使用过程中,有任何问题都可以通过以上链接找寻答案,或者联系我们。

这是一篇自动生成的文章,请删除这篇文章之后开始你的创作吧!

测试代码块:

import java.util.concurrent.*;

public class MyThreadPoolExecutor extends ThreadPoolExecutor {
    public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
                                TimeUnit unit,
                                BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }
    
    public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
                                TimeUnit unit,
                                BlockingQueue<Runnable> workQueue,
                                ThreadFactory threadFactory,
                                RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }
    
    @Override
    protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
        return new FutureTask<T>(runnable, value) {
            final Object rpcContext = XXX.currentRpcContext();
            final long callerId = Thread.currentThread().getId();

            @Override
            public void run() {
                XXX.setRpcContext(rpcContext);
                try {
                    super.run();
                } finally {
                    // 这里很关键
                    if (Thread.currentThread().getId() != callerId) {
                        XXX.clearRpcContext();
                    }
                }
            }
        };
    }
}