Questions tagged [futuretask]

A cancellable asynchronous computation. This class provides a base implementation of Future, with methods to start and cancel a computation, query to see if the computation is complete, and retrieve the result of the computation.

A cancelable asynchronous computation. This class provides a base implementation of Future, with methods to start and cancel a computation, query to see if the computation is complete, and retrieve the result of the computation. The result can only be retrieved when the computation has completed; the get method will block if the computation has not yet completed. Once the computation has completed, the computation cannot be restarted or cancelled.

A FutureTask can be used to wrap a Callable or Runnable object. Because FutureTask implements Runnable, a FutureTask can be submitted to an Executor for execution.

In addition to serving as a standalone class, this class provides protected functionality that may be useful when creating customized task classes. source.

192 questions
52
votes
6 answers

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

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.
Hesey
  • 4,307
  • 5
  • 28
  • 31
47
votes
2 answers

CompletableFuture, supplyAsync() and thenApply()

Need to confirm something. The following code: CompletableFuture .supplyAsync(() -> {return doSomethingAndReturnA();}) .thenApply(a -> convertToB(a)); would be the same as: CompletableFuture .supplyAsync(() -> { A a =…
igr
  • 8,815
  • 10
  • 56
  • 98
45
votes
4 answers

Utility of Future.cancel(boolean) method

I was simply exploring the java.util.concurrent package. I learnt that the class 'Future' has a method boolean cancel(boolean mayInterruptIfRunning) Please find attached the test code I wrote : package com.java.util.concurrent; import…
Kaliyug Antagonist
  • 3,172
  • 8
  • 40
  • 83
37
votes
6 answers

Is it a good way to use java.util.concurrent.FutureTask?

First of all, I must say that I am quite new to the API java.util.concurrent, so maybe what I am doing is completely wrong. What do I want to do? I have a Java application that basically runs 2 separate processing (called myFirstProcess,…
Romain Linsolas
  • 73,921
  • 45
  • 197
  • 265
26
votes
7 answers

How to implement PriorityBlockingQueue with ThreadPoolExecutor and custom tasks

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…
greve
  • 2,471
  • 5
  • 20
  • 31
22
votes
5 answers

How to catch exceptions in FutureTask

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…
Thirler
  • 18,868
  • 13
  • 58
  • 86
14
votes
5 answers

Wait for cancel() on FutureTask

I want to cancel a FutureTask that I get from a ThreadPoolExecutor but I want to be sure the that Callable on the thread pool has stopped it's work. If I call FutureTask#cancel(false) and then get() (to block until completion) I get a…
Jon Tirsen
  • 3,821
  • 3
  • 25
  • 27
12
votes
2 answers

Difference between TimerTask and Executors.newScheduledThreadPool(1)

I need to schedule some work to be done in the future. I can do it in 2 ways: Create a TimerTask and execute timer.schedule(...); Use Executors.newScheduledThreadPool(1): ScheduledExecutorService scheduler =…
Basanth Roy
  • 5,536
  • 4
  • 22
  • 24
11
votes
1 answer

how to convert java Future to guava ListenableFuture

I need to find a way to convert from Future to ListenableFuture. Currently i'm using a service which returns Future but i need to hook up a listener to it. I can't change the service interface as it doesn't belong to me. Is there a simple way to…
justatester
  • 371
  • 2
  • 11
10
votes
3 answers

Getting a result in the future?

I'm looking to get a result from a method which can take a while to complete and doesn't actually return the object, so I'd like to deal with it as effectively as possible. Here's an example of what I'm trying to achieve: public static void main…
Anonomoose
  • 127
  • 1
  • 1
  • 8
10
votes
2 answers

Why exception is null in ThreadPoolExecutor's afterExecute()?

I want to handle exeptions thrown by worker threads in ThreadPoolExecutor#afterExecute() method. Currently I have this code: public class MyExecutor extends ThreadPoolExecutor { public static void main(String[] args) { MyExecutor…
Kaarel Purde
  • 1,145
  • 3
  • 14
  • 30
10
votes
3 answers

Java cancel Future - How to wait for finish?

Long story short: I have a collection of Future objects. Some of them are already in progress, some are not. I iterate the collection and call future.cancel(false) which, according to the documentation, should cancel all Futures that are not…
mdzh
  • 897
  • 1
  • 13
  • 30
9
votes
4 answers

java Callable FutureTask Excecuter: How to listen to finished task

I'm quite new to executer services. Liked doing everything myself, but I think it's time to trust these services. I want to hand by Executer a Runnable. The executer wraps that in a FutureTask and hands it back to me. Now I call poll the done()…
Franz Kafka
  • 9,999
  • 17
  • 86
  • 145
9
votes
4 answers

How to ensure garbage collection of a FutureTask that is submitted to a ThreadPoolExecutor and then cancelled?

I am submitting Callable objects to a ThreadPoolExecutor and they seem to be sticking around in memory. Looking at the heap dump with the MAT tool for Eclipse see that the Callable objects are being referenced by a FutureTask$Sync's callable…
cottonBallPaws
  • 20,217
  • 37
  • 119
  • 169
9
votes
3 answers

How do I get FutureTask to return after TimeoutException?

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…
deltanovember
  • 38,665
  • 55
  • 151
  • 236
1
2 3
12 13