在超时异常后未终止可调用线程上使用 invokeAll 和超时的 ExecutorService

ExecutorService using invokeAll and timeout on callables thread not got terminated after timeout exception

我尝试在线程上设置超时并期望执行程序抛出异常并阻止线程形式 运行 它终止它但超时工作发现情况并非如此 但线程完成执行。 如果超过超时,我该如何终止线程? 这是我的测试代码:

class ArithmeticBB implements ArithmeticManagerCallable.ArithmeticAction {
    @Override
    public String arithmetic(String n) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String ss  = n+" 2" + " ,Thread ID:" +Thread.currentThread().getId();
        return ss;
    }
}
public class ArithmeticManagerCallable {
    ExecutorService executor = null;
    private List<String> integerList = null;
    private List<String> myResult= Collections.synchronizedList(new ArrayList<>());
    private int threadTimeOutInSec = 180;

    public ArithmeticManagerCallable(List<String> dataFromUser, int poolSize, int threadTimeOutInSec) {
        this.integerList =  dataFromUser;
        executor = Executors.newFixedThreadPool(poolSize);
        this.threadTimeOutInSec = threadTimeOutInSec;
    }
    private void exec(ArithmeticAction arithmeticAction) {
        List<String> tempList = new ArrayList<>();
        for(Iterator<String> iterator = integerList.listIterator(); iterator.hasNext();) {
            tempList.add(arithmeticAction.arithmetic(iterator.next()));
        }
        resultArray.addAll(tempList);
    }
    public List<String> invokerActions(List<ArithmeticAction> actions) throws
            InterruptedException {

        Set<Callable<String>> callables = new HashSet<>();
        for (final ArithmeticAction ac : actions) {
            callables.add(new Callable<String>() {
                public String call() throws Exception{
                    exec(ac);
                    return "done";
                }
            });
        }
        List<Future<String>> futures = executor.invokeAll(callables, this.threadTimeOutInSec, TimeUnit.SECONDS);
        executor.shutdown();
        while (!executor.isTerminated()) {
        }
        return myResult;
    }
    public interface ArithmeticAction {
        String arithmetic(String n);
    }

    public static void main(String[] args) {
        List<ArithmeticManagerCallable.ArithmeticAction> actions = new ArrayList();
        actions.add(new ArithmeticBB());
        List<String> intData = new ArrayList<>();
        intData.add("1");

        ArithmeticManagerCallable arithmeticManagerCallable = new ArithmeticManagerCallable(intData,20,4);

        try {
            List<String> result = arithmeticManagerCallable.invokerActions(actions);
            System.out.println("***********************************************");
            for(String i : result) {
                System.out.println(i);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

您的 Thread 在超过超时后未完成执行。

参考这个例子:

ExecutorService executorService = Executors.newFixedThreadPool(20);

List<Callable<String>> callableList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
    final int identifier = i;
    callableList.add(()-> {
        try {
            Thread.sleep(1000 * identifier);
        } catch (InterruptedException e) {
            System.out.println("I'm " + identifier + " and my sleep was interrupted.");
        }
        return identifier + " Hello World";
    });
}

try {
    List<Future<String>> futureList = executorService.invokeAll(callableList, 5, TimeUnit.SECONDS);

    for (Future<String> result : futureList) {
        System.out.println(result.get());
    }
} catch (InterruptedException | ExecutionException e) {
    System.out.println("Something went wrong while executing. This may help: " + e.getMessage());
} catch (CancellationException e) {
    System.out.println("Not all Futures could be received.");
} finally {
    executorService.shutdown();
}