每当工人返回错误价值时,停止其他工人

时间:2014-04-01 07:49:55

标签: java multithreading

我提交了很多在多线程上运行的工人。如果有任何正在运行的工人返回错误值,我们会停止或不提交其他工作人员。下面是我的示例课程演示。我应该在TODO部分做什么?请在这个问题上给我一些建议。

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

public class CompletionServiceTest
{
    public static void main(final String[] args)
    {
        ExecutorService cs = Executors.newFixedThreadPool(1);
        Collection<Worker> tasks = new ArrayList<Worker>(10);

        for(int i=0;i<10;i++) {
            tasks.add(new Worker(i+1));
        }

        List<Future<Boolean>> futures = new ArrayList<Future<Boolean>>(tasks.size());
        try
        {
            for (Callable task : tasks)
            {
                futures.add(cs.submit(task));
            }

        // TODO
       // Check if any false value is returned
      // Then stop all running tasks, no need to run other tasks anymore
       }           
        finally
        {
            //Cancel by interrupting any existing tasks currently running in Executor Service
            for (Future<Boolean> f : futures)
            {
                f.cancel(true);
            }
        }
        System.out.println(new Date()+":Done");
    }
}

class Worker implements Callable<Boolean>
{
    private int number;
    public Worker(int number)
    {
        this.number=number;
    }

    public Boolean call()
        throws InterruptedException
    {
        try
        {            
            Thread.sleep(50000);
            if(number % 4 == 0) {
                return false;
            } else {
                Thread.sleep(500000);
            }
        }
        catch(InterruptedException ie)
        {
            System.out.println("Worker Interuppted");
            throw ie;
        }

        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

我想到了这个解决方案,如果你还在寻找一个有效的例子:

class CompletionServiceTest {

    public static void main(String... args) throws Exception {
        ExecutorService es = newFixedThreadPool(10);
        CompletionService<Boolean> cs = new ExecutorCompletionService<Boolean>(es);
        for (int i = 1; i <= 10; i++) cs.submit(new Worker(i));
        int count = 0;
        while (++count <= 10 && cs.take().get());
        es.shutdownNow();
    }
}

class Worker implements Callable<Boolean> {

    private final int number;

    public Worker(int number) {
        this.number = number;
    }

    @Override public Boolean call() throws Exception {
        try {
            Thread.sleep(1000 + number * 100);
            if (number % 4 == 0) {
                System.out.printf("worker [%d] failed.\n", number);
                return false;
            }
            System.out.printf("worker [%d] done!\n", number);
            return true;
        } catch (InterruptedException ie) {
            System.out.printf("Worker [%d] stopped!\n", number);
            throw ie;
        }
    }
}

输出如下:

worker [1] done!
worker [2] done!
worker [3] done!
worker [4] failed.
Worker [9] stopped!
Worker [7] stopped!
Worker [8] stopped!
Worker [5] stopped!
Worker [6] stopped!
Worker [10] stopped!
相关问题