Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

Future.cancel()

Thread view: 
Matthias Kaeppler - 16 Jun 2005 15:31 GMT
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?

Signature

Matthias Kaeppler

Thomas Weidenfeller - 16 Jun 2005 16:00 GMT
> 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

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq

iksrazal@terra.com.br - 16 Jun 2005 19:28 GMT
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/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.