多个ExecutorCompletionService无法正常工作

时间:2018-11-19 18:12:25

标签: java executorservice java.util.concurrent

我需要从Java应用程序启动和停止任务。我正在尝试使用 用ExecutorService创建线程,用ExecutorCompletionService检查 线程的处理状态。启动和停止是一项持续的活动,因此在我 测试代码我创建了一个while循环。

public class ProcessController {

String[] processArray = { "Process1", "Process2", "Process3", "Process4", "Process5", "Process6", "Process7"      };
private List<String> processList = Arrays.asList(processArray);

public static void main(String[] args ) {

    ExecutorService startUpExecutor = Executors.newFixedThreadPool(3);
    ExecutorService cleanUpExecutor = Executors.newFixedThreadPool(3);

    CompletionService<String> startUpCompletionService = new ExecutorCompletionService<>(startUpExecutor);
    CompletionService<String> cleanUpCompletionService = new ExecutorCompletionService<>(cleanUpExecutor);

    List<Future<String>> cleanupFutures = new ArrayList<Future<String>>();
    List<Future<String>> startupFutures = new ArrayList<Future<String>>();

    ProcessController myApp = new ProcessController();

    int i = 0;

    while (i++ < 3) {
        System.out.println("**********Starting Iteration " + i + "************* =====> ");

        if (!cleanupFutures.isEmpty())   cleanupFutures.clear();


        myApp.processList.forEach(process -> cleanupFutures.add(cleanUpCompletionService.submit(new CleanupTask(process))));
        myApp.processList.forEach(process -> startupFutures.add(startUpCompletionService.submit(new StartupTask(process))));


        for (Future<String> f : cleanupFutures) {
            try {
                String result = cleanUpCompletionService.take().get();
                System.out.println("Result from  Cleanup thread : " + result);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

            for (Future<String> f1 : startupFutures) {
                try {
                    String result = startUpCompletionService.take().get();
                    System.out.println("Result from  startup thread : " + result);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }

            }
            System.out.println("**********Finished Iteration " + i + "************* =====> ");

        }

        startUpExecutor.shutdown();
       cleanUpExecutor.shutdown();
    }
}

CleanupTask类

public class CleanupTask implements Callable<String> {
private String task;

public CleanupTask(String task) {
    this.task = task;
}
@Override
public String call() throws Exception {
    checkIfAnyFinished();
    return "finished clean up processing for " + getThreadId();
}

private void checkIfAnyFinished( )
{
    System.out.println( getThreadId() + " Checking if task   " + this.task + " is finished");
    try {
        isFinished();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

private void isFinished() throws InterruptedException {
    Thread.sleep(1000*4);
}

private String getThreadId()
{
    return Thread.currentThread().getName();
}
}

启动任务类

public class StartupTask implements   Callable<String> {

private String processSchedule ;

public StartupTask(String processSchedule) {
    this.processSchedule = processSchedule;
}

@Override
public String call() {
    scheduleifdue();
    return "finished start up up processing for " + getThreadId();
}

private void scheduleifdue()
{
    System.out.println(getThreadId() + " Checking " + this.processSchedule  + "  is due or not");
    try {
        Thread.sleep(4000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
private String getThreadId()
{
    return Thread.currentThread().getName();
}
}

以上代码成功完成了第1次迭代并开始了第2次迭代。但是它永远不会完成并继续运行。

当我仅使用一个任务(清理或启动)运行同一应用程序时,它运行就没有任何问题。我不确定是什么引起了问题。

0 个答案:

没有答案