【发布时间】:2021-08-14 03:02:53
【问题描述】:
我无法弄清楚如何使用内部异步方法实现方法循环。 当我循环该方法时,它会在方法内的所有异步调用完成之前递增。有没有办法处理这个?代码示例:
void runEngine() {
for(range) {
someAsyncCall();
}
}
void main() {
Runnable r = () -> {
runEngine();
};
while(!stop) {
Thread t1 = new Thread(r);
t1.start();
t1.setDaemon(false);
t1.join();
}
}
详细示例:
package com.abc;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
public class SomeClass{
private final OkHttpClient client = new OkHttpClient();
static void asyncCall() {
Request request = new Request.Builder()
.url("http://publicobject.com/helloworld.txt")
.build();
client.newCall(request).enqueue(new Callback() {
@Override public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override public void onResponse(Call call, Response response) throws IOException {
try (ResponseBody responseBody = response.body()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
Headers responseHeaders = response.headers();
for (int i = 0, size = responseHeaders.size(); i < size; i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
System.out.println(responseBody.string());
}
}
});
}
static void runEngine() {
for(int k = 0; k < 100; k++) {
asyncCall();
}
}
public static void main(String[] args) throws InterruptedException {
Runnable r = () -> {
runEngine();
};
while(true) {
Thread t1 = new Thread(r);
t1.start();
t1.setDaemon(false);
t1.join();
}
}
}
我把我的asyncCall换成了OkHttp网站上的例子,思路是一样的。
【问题讨论】:
-
没有足够的信息来解决这个问题。你能做一个完整的可编译的例子吗?
-
编辑了帖子。我无法完全理解为什么 thread.join() 不适用于带有异步调用的方法。我假设异步调用本身是作为小线程创建的,这可能是问题所在。我做错了什么。
-
是的,您的异步逻辑不一定在您运行可运行文件的同一线程上运行。您可以尝试从该方法返回 CompletableFuture,然后对结果执行类似 CompletableFuture.allof() 的操作。 baeldung.com/java-completablefuture
标签: java multithreading asynchronous