假设我在客户端Java应用程序(即Android应用程序)中有一个方法
ExecutorService pool = ExecutorsService.newFixedThreadPool(1); //1 thread in pool
public void makeHTTPRequestVeryVeryCostly() {
/* IN A BACKGROUND THREAD */
CompleteableFuture<String> s = CompleteableFuture.supplyAsync(() -> makeCostlyReq, pool));
s.get();
updateTheUserUIInTheMainUIThread(s); //Update with s
}
button.onClicklistener((b) -> makeHTTPRequestVeryVeryCostly()); // some button in the UI that will make the http request
在愤怒中,用户点击按钮100次,然后100个请求被提交到ThreadPool中。2件糟糕的事情发生了:
(1)代价高昂的请求计算100次
(2)每次返回后,UI刷新100次。
这些都是非常大的问题。在Java中,解决这个问题的方法是什么?最好是在一个成功的请求之后终止线程池中的所有先前请求,该如何做到这一点呢?
https://stackoverflow.com/questions/57032239
复制相似问题