java.util.concurrent

Future cancel method documentation

柔情痞子 提交于 2019-12-10 10:31:30
问题 According to http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Future.html isDone returns true if cancel(boolean mayInterruptIfRunning) was called. After this method returns, subsequent calls to isDone() will always return true. However, it is possible that task is running and mayInterruptIfRunning is set to false . So, what should return isDone() right after that call? true because of cancel (which is wrong)? Also, it's not clear whether cancel(boolean) method returns false . P.

Why cannot run() of Runnable throw checked Exceptions?

喜欢而已 提交于 2019-12-09 08:33:33
问题 According to section 6.3.2 of JCIP : Runnable is a fairly limiting abstraction; run can not return a value or throw checked exception . run() can not return a value since its return type is void but why can not it throw a checked exception ? 回答1: It cannot throw a checked exception because it wasn't declared as throwing a checked exception from the first version and it is too dangerous to change it. Originally Runnable was only used in a wrapped Thread , and it was assumed the developer would

How to add batching implicit for client?

走远了吗. 提交于 2019-12-08 21:11:20
问题 Lets consider following code: Client code: public class MyClient { private final MyClientSideService myClientSideService; public MyClient(MyClientSideService myClientSideService) { this.myClientSideService = myClientSideService; } public String requestRow(Integer req) { return myClientSideService.requestSingleRow(req); } } Client side service: public class MyClientSideService { private final MyServerSideService myServerSideService; public MyClientSideService(MyServerSideService

What is terminating my Java ExecutorService

爱⌒轻易说出口 提交于 2019-12-08 20:09:09
问题 I originally saw this issue with a more complex subclass of ThreadPoolExecutor , but I have simplified so now contains not much more than some additional debugging, and still get the same problem. import com.jthink.songkong.cmdline.SongKong; import com.jthink.songkong.ui.MainWindow; import com.jthink.songkong.util.SongKongThreadFactory; import java.util.concurrent.*; import java.util.logging.Level; public class TimeoutThreadPoolExecutor extends ThreadPoolExecutor { /** * Uses the default

understanding java's synchronized collections

偶尔善良 提交于 2019-12-08 02:37:46
问题 I'm reading the java official doc regarding wrappers implementation, which are static methods in Collections used to get synchronized collection, for example : List<Type> list = Collections.synchronizedList(new ArrayList<Type>()); ... the thing that I did not understand is the following (I quote from the java doc ) : A collection created in this fashion is every bit as thread-safe as a normally synchronized collection, such as a Vector. In the face of concurrent access, it is imperative that

Java 8: How can I convert a for loop to run in parallel?

老子叫甜甜 提交于 2019-12-08 01:06:41
问题 for (int i=0; i<100000; i++) { // REST API request. restTemplate.exchange(url, HttpMethod.GET, request, String.class); } I have a situation where I have to request a resource for 100k users and it takes 70 minutes to finish. I tried to clean up my code as much as possible and I was able to reduce it only by 4 minutes). Since each request is independent of each other, I would love to send requests in parallel (may be in 10s, 100s, or even 1000s of chunks which every finishes quickly). I'm

Exception propagation in java.util.concurrent.CompletableFuture

懵懂的女人 提交于 2019-12-07 14:56:09
问题 There are two snippets of code. In the first one we create the CompletableFuture from the task which always throws some exception. Then we apply "exceptionally" method to this future, then "theAccept" method. We DO NOT assign new future returned by theAccept method to any variable. Then we invoke "join" on original future. What we see is that "exceptionally" method has been invoked as well as the "thenAccept". We see It because they printed appropriate lines in output. But the Exception has

List<Runnable> returned from shutdownNow() can not be converted to submitted Runnable

两盒软妹~` 提交于 2019-12-07 12:00:54
问题 Below is the source code of the class. I wanted to verify how does shutdownNow() works for not submitted task. Problem I am getting in below code is shutdownNow() return List<FutureTask> and not List<Runnable> which I have submitted List<Runnable> containing submitted instance of PrimeProducer . In Below program I wanted to get the tasks which where not executed and their state so that I can reschedule them later. name() represents just state that I want to store. So I am not able to convert

How to make a ScheduledExecutorService terminate automatically when its scheduled task is cancelled

大城市里の小女人 提交于 2019-12-07 08:34:28
问题 I'm using a ScheduledExecutorService to close a network connection if it has been open for more than several hours. In most cases however, the network connection is closed before the timeout is reached, so I cancel the ScheduledFuture . In this case, I also want the executor service to terminate and to release its thread pool. To my surprise, this does not work out of the box: Although I have called shutdown() on the executor service after scheduling the task, the executor service does not

Clever asynchronous repaint in Java

≡放荡痞女 提交于 2019-12-07 01:21:33
问题 I have a use-case coming from a GUI problem I would like to submit to your sagacity. Use case I have a GUI that displays a computation result depending on some parameters the user set in a GUI. For instance, when the user moves a slider, several events are fired, that all trigger a new computation. When the user adjust the slider value from A to B, a dozens of events are fired. But the computation can take up to several seconds, whereas the slider adjustment can fire an event every few 100 ms