ExecutorService在调用线程中运行任务? [英] ExecutorService which runs tasks in calling thread?

查看:100
本文介绍了ExecutorService在调用线程中运行任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何java.util.ExecutorService实现仅在调用线程中运行所有已执行的任务?如果默认情况下Java中未包含此代码,是否有一个包含此类实现的库?

Are there any java.util.ExecutorService implementations which simply run all executed tasks in the calling thread? If this isn't included in Java by default, is there a library which contains an implementation like this?

推荐答案

我能找到的唯一现有实现是 camel 库.

The only existing implementation I could find is SynchronousExecutorService - unfortunately buried somewhere in camel library.

在此处粘贴源代码(不带注释)以供将来参考:

Pasting source code (without comments) here for future reference:

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();
    }

}

这篇关于ExecutorService在调用线程中运行任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆