I posted a question on about brute forcingCodeReview, 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. I copied one example that I understood and made few changes to fit my need. The code works perfectly till I read deep about multithreading and came across invokeAny(), I tried to implement it and thus the beginning of my headache for some unknown reason it's giving me errors. I just want to be able to get the first task that find the resolved ID using invokeAny, since the program is fast now.
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;
}
}