ExecutorService的运行在调用线程的任务?(ExecutorService which

2019-06-27 01:12发布

是否有任何java.util.ExecutorService实现它只是运行在调用线程的所有执行的任务? 如果这不是默认包含在Java中,有没有包含这样一种方式的图书馆吗?

Answer 1:

现有的唯一实现我能找到是SynchronousExecutorService -可惜的地方埋在骆驼图书馆。

粘贴的源代码(没有评论)在这里以供将来参考:

package org.apache.camel.util.concurrent;

import java.util.List;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.TimeUnit;

public class SynchronousExecutorService extends AbstractExecutorService {

    private volatile boolean shutdown;

    public void shutdown() {
        shutdown = true;
    }

    public List<Runnable> shutdownNow() {
        return null;
    }

    public boolean isShutdown() {
        return shutdown;
    }

    public boolean isTerminated() {
        return shutdown;
    }

    public boolean awaitTermination(long time, TimeUnit unit) throws InterruptedException {
        return true;
    }

    public void execute(Runnable runnable) {
        runnable.run();
    }

}


Answer 2:

您可以使用番石榴com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor(),这不正是你想要什么: https://github.com/google/guava/blob/0434c5199c83c3f43b8b6a86c62e121d518fe7d0/guava/src/com/google/通用/ UTIL /并发/ MoreExecutors.java#L267

编辑:该方法已更名为com.google.common.util.concurrent.MoreExecutors.newDirectExecutorService() https://github.com/google/guava/blob/f67ab864bde63fa6602b5688de0440957ffeaa2e/guava/src/com/google/common/util /concurrent/MoreExecutors.java#L369



Answer 3:

你看着java.util.concurrent.ThreadPoolExecutor中?



文章来源: ExecutorService which runs tasks in calling thread?