futuretask

How to catch exceptions in FutureTask

时光总嘲笑我的痴心妄想 提交于 2019-11-27 22:46:07
After finding that FutureTask running in a Executors.newCachedThreadPool() on Java 1.6 (and from Eclipse) swallows exceptions in the Runnable.run() method, I've tried to come up with a way to catch these without adding throw/catch to all my Runnable implementations. The API suggests that overriding FutureTask.setException() should help in this: Causes this future to report an ExecutionException with the given throwable as its cause, unless this Future has already been set or has been cancelled. This method is invoked internally by the run method upon failure of the computation. However this

CompletableFuture, supplyAsync() and thenApply()

為{幸葍}努か 提交于 2019-11-27 11:06:18
问题 Need to confirm something. The following code: CompletableFuture .supplyAsync(() -> {return doSomethingAndReturnA();}) .thenApply(a -> convertToB(a)); would be the same as: CompletableFuture .supplyAsync(() -> { A a = doSomethingAndReturnA(); convertToB(a); }); Right? Furthermore, another two questions following as for "is there any reason why we would use thenApply ?" 1) having big code for conversion? or 2) need to reuse the lambda block in other places? 回答1: It is not the same thing . In

What's the difference between Future and FutureTask in Java?

心已入冬 提交于 2019-11-27 10:44:07
问题 Since use ExecutorService can submit a Callable task and return a Future , why need to use FutureTask to wrap Callable task and use the method execute ? I feel they both do the same thing. 回答1: In fact you are correct. The two approaches are identical. You generally don't need to wrap them yourself. If you are, you're likely duplicating the code in AbstractExecutorService: /** * Returns a <tt>RunnableFuture</tt> for the given callable task. * * @param callable the callable task being wrapped

How do I get FutureTask to return after TimeoutException?

家住魔仙堡 提交于 2019-11-27 02:55:47
问题 In the code below, I'm catching a TimeoutException after 100 seconds as intended. At this point I would expect the code to exit from main and the program to terminate but it keeps printing to the console. How do I get the task to stop executing after timeout? private static final ExecutorService THREAD_POOL = Executors.newCachedThreadPool(); private static <T> T timedCall(Callable<T> c, long timeout, TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException {

How to catch exceptions in FutureTask

会有一股神秘感。 提交于 2019-11-26 23:12:57
问题 After finding that FutureTask running in a Executors.newCachedThreadPool() on Java 1.6 (and from Eclipse) swallows exceptions in the Runnable.run() method, I've tried to come up with a way to catch these without adding throw/catch to all my Runnable implementations. The API suggests that overriding FutureTask.setException() should help in this: Causes this future to report an ExecutionException with the given throwable as its cause, unless this Future has already been set or has been

How to implement PriorityBlockingQueue with ThreadPoolExecutor and custom tasks

天涯浪子 提交于 2019-11-26 22:20:53
问题 I've searched a lot but could not find a solutuion to my problem. I have my own class, BaseTask , that uses a ThreadPoolExecutor to handle tasks. I want task prioritization, but when I try to use a PriorityBlockingQueue I get ClassCastException because the ThreadPoolExecutor wraps my Tasks into a FutureTask object. This obviously makes sense because the FutureTask does not implement Comparable , but how would I go on to solve the priority problem? I've read that you could override newTaskFor(