如何修复 concurrent.ExecutionException 原因:java.lang.UnsupportedOperationException

How to fix concurrent.ExecutionException Caused by: java.lang.UnsupportedOperationException

我发布了一个关于暴力破解的问题CodeReview, and a good expert helped me out by suggesting I should use multithreading to improve the speed of my program. The code he gave was good, improved the speed but was still slow for me, so I went to research more about multithreading and found good articles and examples。我复制了一个我理解的例子,并做了一些改动以满足我的需要。在我深入阅读多线程并遇到 invokeAny() 之前,代码一直运行良好,我尝试实现它,因此我开始头疼,因为一些未知的原因它给了我错误。我只想能够使用 invokeAny 获得第一个找到已解析 ID 的任务,因为程序现在很快了。

Exception in thread "main" java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException: Not supported yet.
at java.base/java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.base/java.util.concurrent.FutureTask.get(FutureTask.java:191)
at java.base/java.util.concurrent.AbstractExecutorService.doInvokeAny(AbstractExecutorService.java:199)
at java.base/java.util.concurrent.AbstractExecutorService.invokeAny(AbstractExecutorService.java:220)
at pait.SiimpleeThreadPool.main(SiimpleeThreadPool.java:31)
Caused by: java.lang.UnsupportedOperationException: Not supported yet.
at pait.WorkerThread.call(WorkerThread.java:73)
at pait.WorkerThread.call(WorkerThread.java:15)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.awt.Label;
import java.util.Random;
import java.util.concurrent.Callable;
public class SimpleThreadPool {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        int s = 135000000;
        int e = 200000000;
        List<Callable<String>> callList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Callable worker = new WorkerThread("" + i, s, e);
            callList.add(worker);
            s = s + 15520000;
        }
        String result = executor.invokeAny(callList);
        System.out.println(result);
        executor.shutdown();
        while (!executor.isTerminated()) {
        }
        System.out.println("Finished all threads");
    }

}

public class WorkerThread implements Callable {

    private final  String command;
    private final int start;
    private final int end;
    private final Label lb = new Label();

    public WorkerThread(String s, int start, int end) {
        this.command = s;
        this.start = start;
        this.end = end;
    }

    public void run() {
        processCommand(start, end);

    }

    private void processCommand(int start, int end) {
        for (int i = start; i < end; i++) {
            Random rand= new Random(i);
            long pair = rand.nextInt();
            if (pair == 739619665) {
                System.out.println(start + "     " + end + "        Executing Task inside : " + Thread.currentThread().getName());
                System.out.println(i);
                lb.setText("Stop");
                break;
            }
        }
    }

    ;

    @Override
    public String call() throws Exception {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public String toString() {
        return this.command;
    }

}

为什么 UnsupportedOperationException

您的 WorkerThread class 实现了 Callable 接口,即:

A task that returns a result and may throw an exception. Implementors define a single method with no arguments called call.

使用 Callable 实现的 API,例如 ExecutorService#invokeAny(Collection),将调用 call 方法,这意味着实际工作需要在上述 call 方法中实现.但是,您的代码如下:

@Override
public String call() throws Exception {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

这就是您得到 UnsupportedOperationException 的原因,因为这正是该方法的目的。要解决此问题,您需要简单地正确实施该方法。您还有:

public void run() {
    processCommand(start, end);

}

这让我觉得你要么混淆了 RunnableCallable 接口,要么混淆了用于实现 RunnableWorkerThread class 而你忘记了切换到 Callable 时完全更新代码。假设 processCommand 是任务的实际工作,您的 call 方法应该类似于:

@Override
public String call() throws Exception {
    processCommand(start, end);
    return "I don't know what's supposed to be returned";
}

并且您可以删除 run 方法。


等待终止

您不应该等待 ExecutorService 终止:

while (!executor.isTerminated()) {
}

该接口提供了一种方法,该方法会阻塞直到 ExecutorService 终止,这通常 CPU 更友好:ExecutorService#awaitTermination(long,TimeUnit)。该方法接受超时值以指定线程应阻塞等待终止的最长时间。如果要等待 "forever" 使用非常大的值,例如:

executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);

避免原始类型

我推荐阅读What is a raw type and why shouldn't we use it?Callable 接口是通用的,但在某些地方您使用的是原始类型。而不是:

public class WorkerThread implements Callable {...}
// and
Callable worker = new WorkerThread(...);

你应该有:

public class WorkerThread implements Callable<String> {...}
// and
Callable<String> worker = new WorkerThread<>(...);

UI线程

请注意,您有一个 java.awt.Label 正在 processCommand 方法中更新。请记住,UI 对象通常 必须 只能在 UI 线程上访问(例如 事件调度线程 (EDT) 在 Swing 中)。至少,Swing 就是这种情况——我不确定 AWT 是否是线程安全的。也就是说,我实际上不确定您为什么使用 Label,因为您的代码 none 似乎与 GUI.

有关