在AJAX中,假设我异步提交请求。当reposne返回时,它执行一个回调函数。多线程环境中的java回调函数
在java多线程环境中实现相同的最佳方式是什么? 即主线程创建一个子线程并提交一个任务,然后子线程返回一个回调函数需要由主线程执行。
这可能吗?在主线程中,我可以执行wait(),并且在子线程中,我可以执行notify(),但在这种情况下,主线程将等待直到子线程完成。 但在AJAX主线程继续工作......这就是我想要
在AJAX中,假设我异步提交请求。当reposne返回时,它执行一个回调函数。多线程环境中的java回调函数
在java多线程环境中实现相同的最佳方式是什么? 即主线程创建一个子线程并提交一个任务,然后子线程返回一个回调函数需要由主线程执行。
这可能吗?在主线程中,我可以执行wait(),并且在子线程中,我可以执行notify(),但在这种情况下,主线程将等待直到子线程完成。 但在AJAX主线程继续工作......这就是我想要
你可以使用一个ExecutorService
做的后台任务,然后使用的Future
方法你回到等待为结果。例如:
class Main {
private static final ExecutorService es = Executors.newCachedThreadPool();
public static void main(final String... args) throws Throwable {
List<Future<Integer>> results = new ArrayList<>();
for (int i = 0; i < 10; i++) {
results.add(asyncSum(i, i*i));
}
// here, in the main thread, you can do whatever you want
// while the calculations are performed in background threads
// ...
// after the main thread finishes what it was doing, it
// can process the futures
for (final Future<Integer> result : results) {
System.out.println(result.get());
}
}
// this method launches a calculation on a worker thread and immediately
// returns a Future, which is a reference to the result of the calculation
// once it is completed
private static Future<Integer> asyncSum(final int a, final int b) {
return es.submit(new Callable<Integer>() {
@Override public Integer call() throws Exception {
return a + b;
}
});
}
}
在上面的例子中,主线程将阻塞,直到第一次计算完成,然后打印它。然后阻塞,直到第二次计算完成,然后打印等
如果你要打印的结果,因为它们变得可用(未指定顺序),那么你可以使用一个CompletionService
,而是具有结果列表并对其进行迭代,您可以通过完成服务本身通过.take()
方法获得期货,该方法阻止直到计算完成;或者.poll()
,如果存在完成计算,则返回Future;如果存在完成计算,则返回null没有完成计算 - 这样你的主线程将永远不会阻塞。如下示例使用CompletionService
。它显示了永不阻挡的主线程,使用后台线程进行计算并在结果可用时处理结果:
class Main {
public static void main(String[] args) throws Throwable {
final ExecutorService es = Executors.newCachedThreadPool();
final CompletionService<Integer> cs = new ExecutorCompletionService<>(es);
submitSomeCalculations(cs);
while (true) {
doMainThreadWork();
processFinishedCalculations(cs);
}
}
private static void submitSomeCalculations(final CompletionService<Integer> cs) {
for (int i = 0; i < 10; i++) {
submitAsyncSum(cs, i, i * i);
}
}
private static void submitAsyncSum(final CompletionService<Integer> cs, final int a, final int b) {
cs.submit(new Callable<Integer>() {
@Override public Integer call() throws Exception {
Thread.sleep(100 + (long) (Math.random() * 900));
return a + b;
}
});
}
private static void processFinishedCalculations(final CompletionService<Integer> cs) throws ExecutionException, InterruptedException {
while (true) {
final Future<Integer> result = cs.poll();
if (result == null) {
System.out.println("> no finished results...");
break;
} else {
System.out.println("> result available: " + result.get());
}
}
}
static void doMainThreadWork() {
System.out.println("work from main thread...");
}
}