gpt4 book ai didi

java - 如何修复由 : java. lang.UnsupportedOperationException 引起的并发.ExecutionException

转载 作者:行者123 更新时间:2023-12-02 09:25:13 30 4
gpt4 key购买 nike

我发布了一个关于暴力破解的问题 CodeReview ,一个好expert通过建议我应该使用多线程来提高程序速度来帮助我。他给出的代码很好,提高了速度,但对我来说仍然很慢,所以我去研究更多关于多线程的知识,发现很好articlesexamples 。我复制了一个我理解的示例,并做了一些更改以满足我的需要。该代码工作得很好,直到我深入了解多线程并遇到了 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 类实现 Callable接口(interface),即:

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);

}

这让我认为您要么对 Runnable 和 Callable 接口(interface)或用于实现 Runnable 的 WorkerThread 类感到困惑 并且在切换到 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()) {
}

该接口(interface)提供了一种方法,该方法会阻塞直到 ExecutorService 终止,这通常对 CPU 更友好:ExecutorService#awaitTermination(long,TimeUnit) 。该方法接受超时值来指定线程应阻塞等待终止的最长时间。如果您想“永远”等待,请使用一个非常大的值,例如:

executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
<小时/>

避免原始类型

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

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

你应该有:

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

用户界面线程

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

关于java - 如何修复由 : java. lang.UnsupportedOperationException 引起的并发.ExecutionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58387614/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com