标签: completion-service

我应该何时在ExecutorService上使用CompletionService?

我刚刚在这篇博客文章中找到了CompletionService .但是,这并没有真正展示CompletionService相对于标准ExecutorService的优势.可以用任何一个编写相同的代码.那么,什么时候CompletionService有用呢?

你能给一个简短的代码样本,使它清晰吗?例如,此代码示例仅显示不需要CompletionService的位置(=等效于ExecutorService)

    ExecutorService taskExecutor = Executors.newCachedThreadPool();
    //        CompletionService<Long> taskCompletionService =
    //                new ExecutorCompletionService<Long>(taskExecutor);
    Callable<Long> callable = new Callable<Long>() {
        @Override
        public Long call() throws Exception {
            return 1L;
        }
    };

    Future<Long> future = // taskCompletionService.submit(callable);
        taskExecutor.submit(callable);

    while (!future.isDone()) {
        // Do some work...
        System.out.println("Working on something...");
    }
    try {
        System.out.println(future.get());
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

java concurrency multithreading completion-service

74
推荐指数
5
解决办法
5万
查看次数

Spring相当于CompletionService?

在我的应用程序中,我必须从主应用程序线程异步处理多个作业并收集每个作业的结果.我有一个普通的Java解决方案,它使用ExecutorService和ExecutorCompletionService来收集作业结果.

现在我想将我的代码转换为Spring解决方案.该文档显示我使用的ExecutorService和@Async注释,但如何,我不知道如何,如果能收集多个作业的结果.

换句话说:我正在寻找与CompletionService相当的Spring.有这样的事吗?

我目前的代码:

class MyService {

private static ExecutorService executorService;
private static CompletionService<String> taskCompletionService;

// static init block
static {
    executorService = Executors.newFixedThreadPool(4);
    taskCompletionService = new ExecutorCompletionService<String>(executorService);

    // Create thread that keeps looking for results
    new Thread(new Runnable() {

        @Override
        public void run() {
            while (true) {
                try {
                    Future<String> future = taskCompletionService.take();
                    String s = future.get();
                    LOG.debug(s);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }

    }).start();
}

// This …
Run Code Online (Sandbox Code Playgroud)

spring multithreading executorservice completion-service

5
推荐指数
1
解决办法
1722
查看次数

有没有办法将 ScheduledExecutorService 与 ExecutorCompletionService 一起使用?

我正在尝试一起使用 ExecutorCompletionService 和 ScheduledExecutorService 。

我需要做的是安排不同的活动,每个活动都有“执行前的延迟”,然后根据上次运行的结果“重新安排它们”(不同的延迟)。

我遇到的问题是我不能使用 ExecutorCompletionService 提交“延迟”

我尝试了以下操作,但它会永远阻塞...

显然,我遗漏了 Java 语言中的一个基本问题。

有没有将任务安排到 ScheduledExecutorService 以便 CompletionService“知道”?

public class Bar {

    private ScheduledExecutorService scheduledExecutor;
    private Future<Status> action1Future;
    private Future<Status> action2Future;
    private ExecutorCompletionService<Status> pool;
    private long delay1 = 10;
    private long delay2 = 20;
    private long delay3 = 30;

    public void start() {
        scheduledExecutor = Executors.newScheduledThreadPool(3);

        Action1 a1 = new ActionOne(); // Action1 implements Callable<Status>
        Action2 a2 = new ActionTwo(); // Action2 implements Callable<Status>

        pool = new ExecutorCompletionService<Status>(scheduledExecutor);
        action1Future = scheduledExecutor.schedule(a1, …
Run Code Online (Sandbox Code Playgroud)

java multithreading completion-service

5
推荐指数
1
解决办法
1623
查看次数