gpt4 book ai didi

java - 在 JAVA Servlet 中将 Runnable.run() 转换为 Callable.call()

转载 作者:搜寻专家 更新时间:2023-11-01 02:25:21 26 4
gpt4 key购买 nike

我在将具有可运行接口(interface)的代码转换为以下代码中的可调用接口(interface)时遇到问题。我需要更改,因为我需要通过线程返回一个 Sting[][] isR

当我只是将接口(interface)更改为 callable 并将 chande .run() 更改为 .call() 时,然后 new Thread (new Worker(startSignal, doneSignal, i)).start(); 不会工作。

CountDownLatch startSignal = new CountDownLatch(1);
CountDownLatch doneSignal = new CountDownLatch(3); // 3 tasks

class Worker implements Runnable {
private final CountDownLatch startSignal;
private final CountDownLatch doneSignal;
private final int threadNumber;

// you can pass additional arguments as well
Worker(CountDownLatch startSignal, CountDownLatch doneSignal, int threadNumber) {
this.startSignal = startSignal;
this.doneSignal = doneSignal;
this.threadNumber = threadNumber;
}

public void run() {
try {
startSignal.await();

if (threadNumber == 1) {
String[][] isRs = getIS(erg1, erg2, request);
}

if (threadNumber == 2) {
getIW(erg1, erg2, request);
}

if (threadNumber == 3) {
getIN(search_plz, request);
}

doneSignal.countDown();
} catch (InterruptedException ex) {
System.out.println(ex);
}
}
}

// 3 new threads are started
for (int i = 1; i <= 3; i++) {
new Thread(new Worker(startSignal, doneSignal, i)).start();
}

startSignal.countDown(); // let all threads proceed
try {
doneSignal.await(); // wait for all to finish
// all 3 tasks are finished and do whatever you want to do next
} catch (Exception e) {

}

最佳答案

您不能传递 Callable进入Thread执行。

使用 ExecutorService执行 Callable目的。

可以给Callable使用其 submit() 运行的对象方法:

<T> Future<T> submit(Callable<T> task)

你的类应该是这样的:

class Worker {

private final CountDownLatch startSignal;
private final CountDownLatch doneSignal;
private final int threadNumber;

Worker(
CountDownLatch startSignal,
CountDownLatch doneSignal,
int threadNumber
){

this.startSignal = startSignal;
this.doneSignal = doneSignal;
this.threadNumber = threadNumber;

}

public String[][] getSomeStrArrArr() {

try {

startSignal.await();

if (threadNumber == 1) {
System.out.println("Running thread number 1");
}

if (threadNumber == 2) {
System.out.println("Running thread number 2");
}

if (threadNumber == 3) {
System.out.println("Running thread number 3");
}

doneSignal.countDown();

} catch (InterruptedException ex) {

System.out.println(
"Thread number "+threadNumber+" has been interrupted."
);

}

// replace these 2 lines with the actual code to get the String[][]
String[][] someStrArrArr = new String[1][1];
someStrArrArr[0][0] = "Done with thread number "+threadNumber;

return someStrArrArr;

}

public Callable<String[][]> getSomeCallableStrArrArr(){
return new Callable<String[][]>() {
public String[][] call() throws Exception {
return getSomeStrArrArr();
}
};
}

}

你会这样开始:

    ExecutorService pool = Executors.newFixedThreadPool(3);
Set<Future<String[][]>> set = new HashSet<Future<String[][]>>();
CountDownLatch startSignal = new CountDownLatch(1);
CountDownLatch doneSignal = new CountDownLatch(3);
for (int i=1;i<=3;i++) {
Worker worker = new Worker(startSignal,doneSignal,i);
Callable<String[][]> callable =
worker.getSomeCallableStrArrArr();
Future<String[][]> future = pool.submit(callable);
set.add(future);
}

然后,获取并打印结果字符串:

    for(Future<String[][]> future : set){
String[][] result = future.get();
for (String[] strArr: result){
for (String str: strArr){
System.out.println(str);
}
}
}

但是这个设计可以改进。查看以下有关 Callable 的文档看看它与 Runnable 有何不同以及如何从这些差异中获益并正确实现:

Interface Callable

https://blogs.oracle.com/CoreJavaTechTips/entry/get_netbeans_6?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+corejavatechtips+(Core+Java+Technologies+Tech+Tips)

另请查看此链接,其中我根据您的代码编写了一个示例,您可以运行和修改:http://ideone.com/blUQm0

关于java - 在 JAVA Servlet 中将 Runnable.run() 转换为 Callable.call(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25094333/

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