执行者没有完成所有任务

Executors not completing all the task

嗨,我正在尝试 运行 下面的代码,在执行程序终止后,我希望剩余任务的计数为 0,但出于某种原因,当它满足以下条件时,它会超过 100。

 while(executor.isTerminated()) {
                System.out.println("Total Task Remaining : " + ExecutorServiceExample.task.size());
                System.out.println("*** Executor Terminated ***");
                break;
            }

代码片段。

package test;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ExecutorServiceExample {

    public static volatile Set<String> task = new HashSet<String>();

    public static void main(String args[]) {

        ExecutorService executor = Executors.newFixedThreadPool(2000);

        for (int i = 0; i < 10000; i++) {
            String name = "task#" + i;
            task.add(name);
            Runnable runner = new TaskPrint(name);
            executor.execute(runner);
        }

        try {
            executor.shutdown();
            executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
            if (executor.isTerminated()) {
                System.out.println("Total Task Remaining : " + ExecutorServiceExample.task.size());
                System.out.println("*** Executor Terminated ***");
            }
        } catch (InterruptedException ignored) {
        }
    }
}

class TaskPrint implements Runnable {

    private final String name;

    public TaskPrint(String name) {
        this.name = name;
    }

    public void run() {
        ExecutorServiceExample.task.remove(name);
    }
}

基于任务数量的结果有些奇怪。

100 个任务的输出。

Total Task Remaining : 0
*** Executor Terminated ***

1000 个任务的输出。

Total Task Remaining : 0
*** Executor Terminated ***

10000 个任务的输出。

Total Task Remaining : -27
*** Executor Terminated ***

100000 个任务的输出。

Total Task Remaining : 1205
*** Executor Terminated ***

HashSet 不是线程安全的。您可以使用

创建一个 synchronizedSet
public static volatile Set<String> task = Collections.synchronizedSet(new HashSet<String>());