Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Future.cancel()

Reply
Thread Tools

Future.cancel()

 
 
Matthias Kaeppler
Guest
Posts: n/a
 
      06-16-2005
Hi,

I was wondering if this method, as implemented by e.g. the FutureTask
class, allows me to terminate a thread?
I created an ExecutorService and submit()-ed a Future object, which I
initialized with another object implementing runnable. The thread is
started correctly, but calling cancel() on this future object doesn't
seem to have any effect (although it returns true, indicating that
cancellation of the task succeeded).

How come?

--
Matthias Kaeppler
 
Reply With Quote
 
 
 
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      06-16-2005
Matthias Kaeppler wrote:
> I was wondering if this method, as implemented by e.g. the FutureTask
> class, allows me to terminate a thread?


No, you expect miracles. The concurrency classes are implemented in Java
and can't do more than you could do in own code. You will notice that
the API documentation talks about an *attempt* (not a guarantee) to
cancel the *task* (not the thread). After a call to cancel() the task
will claim to have been canceled - even if there is still processing
going on in the background. cancel() will, if permitted, try to
interrupt the underlying thread. It depends on the code in the thread
what happens there.


> I created an ExecutorService and submit()-ed a Future object, which I
> initialized with another object implementing runnable. The thread is
> started correctly, but calling cancel() on this future object doesn't
> seem to have any effect (although it returns true, indicating that
> cancellation of the task succeeded).


It has the desired effect: Stating that you are no longer interested in
the result from the Future. Nothing more. As a courtesy, cancel()
informs the backend to stop trying to calculate the result (it is only
an *attempt*), but the backend is free to run to an end and produce a
result - a result which will then be discarded by the Future, because
you told it you no longer need it.

> How come?


No miracles.

/Thomas


--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
 
Reply With Quote
 
 
 
 
iksrazal@terra.com.br
Guest
Posts: n/a
 
      06-16-2005
The way I've done it is by scheduling a TimerTask to to cancel the
task, overload done() and perform the operation if isCancelled returns
true. I got the idea from the concurrency-interest list where the
subject comes up occasionally.

HTH,
iksrazal
http://www.braziloutsource.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments