我发布了一个关于暴力强迫CodeReview的问题,一位优秀的专家帮助我建议我应该使用多线程来提高程序的速度。他给出的代码很好,提高了速度,但对我来说仍然很慢,所以我去研究更多关于多线程的信息,并找到了很好的文章和示例。我复制了一个我理解的示例,并做了一些更改以满足我的需求。代码运行良好,直到我深入阅读多线程并遇到 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
接口,即:
返回结果并可能引发异常的任务。实现者定义一个没有参数的方法,称为
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
接口之间感到困惑,要么在用于实现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()) {
}
该接口提供了一种在ExecutorService
终止之前阻塞的方法,该方法通常对 CPU 更友好:ExecutorService#awaitTermination(long,TimeUnit)
。该方法接受超时值,以指定线程应阻止等待终止的最长时间。如果要"永远"等待,请使用非常大的值,例如:
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
避免原始类型
我建议阅读什么是原始类型,为什么我们不应该使用它?。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 线程上访问(例如 Swing 中的事件调度线程 (EDT(。至少,Swing 就是这种情况——我不确定 AWT 是否是线程安全的。也就是说,我实际上不确定您为什么要使用Label
,因为您的代码似乎都与 GUI 无关。