gpt4 book ai didi

java - 获取 Java 中预定的非阻塞操作的结果

转载 作者:行者123 更新时间:2023-11-30 05:35:57 27 4
gpt4 key购买 nike

我正在尝试以预定且非阻塞的方式执行一些阻塞操作(例如 HTTP 请求)。假设我有 10 个请求,其中一个请求需要 3 秒,但我不想等待 3 秒,而是等待 1 秒并发送下一个请求。所有执行完成后,我想将所有结果收集在一个列表中并返回给用户。

下面是我的场景的原型(prototype)(线程 sleep 用作阻塞操作而不是 HTTP 请求。)

    public static List<Integer> getResults(List<Integer> inputs) throws InterruptedException, ExecutionException {
List<Integer> results = new LinkedList<Integer>();
Queue<Callable<Integer>> tasks = new LinkedList<Callable<Integer>>();
List<Future<Integer>> futures = new LinkedList<Future<Integer>>();
for (Integer input : inputs) {
Callable<Integer> task = new Callable<Integer>() {
public Integer call() throws InterruptedException {
Thread.sleep(3000);
return input + 1000;
}
};
tasks.add(task);
}

ExecutorService es = Executors.newCachedThreadPool();
ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
ses.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
Callable<Integer> task = tasks.poll();
if (task == null) {
ses.shutdown();
es.shutdown();
return;
}
futures.add(es.submit(task));
}
}, 0, 1000, TimeUnit.MILLISECONDS);

while(true) {
if(futures.size() == inputs.size()) {
for (Future<Integer> future : futures) {
Integer result = future.get();
results.add(result);
}
return results;
}
}
}

public static void main(String[] args) throws InterruptedException, ExecutionException {
List<Integer> results = getResults(new LinkedList<Integer>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)));
System.out.println(Arrays.toString(results.toArray()));
}

我正在等待 while 循环,直到所有任务返回正确的结果。但它永远不会进入中断条件并且无限循环。每当我放置像记录器这样的 I/O 操作甚至断点时,它只会中断 while 循环,一切都会变得正常。

我对 Java 并发性比较陌生,并试图了解正在发生的事情以及这是否是正确的方法。我猜想 I/O 操作会触发线程调度程序上的某些内容并使其检查集合的大小。

最佳答案

您需要同步您的线程。您有两个不同的线程(主线程和执行服务线程)访问 futures列表和自 LinkedList不同步,这两个线程看到 futures 的两个不同值.

while(true) {
synchronized(futures) {
if(futures.size() == inputs.size()) {
...
}
}
}

发生这种情况是因为 java 中的线程使用 cpu 缓存来提高性能。因此,每个线程可以具有不同的变量值,直到它们同步为止。这个所以question有更多相关信息。

同样来自this答案:

It's all about memory. Threads communicate through shared memory, but when there are multiple CPUs in a system, all trying to access the same memory system, then the memory system becomes a bottleneck. Therefore, the CPUs in a typical multi-CPU computer are allowed to delay, re-order, and cache memory operations in order to speed things up.

That works great when threads are not interacting with one another, but it causes problems when they actually do want to interact: If thread A stores a value into an ordinary variable, Java makes no guarantee about when (or even if) thread B will see the value change.

In order to overcome that problem when it's important, Java gives you certain means of synchronizing threads. That is, getting the threads to agree on the state of the program's memory. The volatile keyword and the synchronized keyword are two means of establishing synchronization between threads.

最后,futures列表不会在您的代码中更新,因为主线程由于 infinte while 而持续被占用。堵塞。在 while 循环中执行任何 I/O 操作都会为 CPU 提供足够的喘息空间来更新其本地缓存。

无限 while 循环通常不是一个好主意,因为它非常消耗资源。在下一次迭代之前添加一个小的延迟可以让它好一点(尽管仍然效率低下)。

关于java - 获取 Java 中预定的非阻塞操作的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56628357/

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