编程语言
首页 > 编程语言> > java – 在调用线程中运行任务的ExecutorService?

java – 在调用线程中运行任务的ExecutorService?

作者:互联网

是否有任何java.util.ExecutorService实现只是在调用线程中运行所有已执行的任务?如果默认情况下这不包含在Java中,那么是否有一个包含这样的实现的库?

解决方法:

我能找到的唯一现有实现是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();
    }

}

标签:java,executorservice,threadpool,camel
来源: https://codeday.me/bug/20190613/1232218.html