Future.cancel()

Discussion in 'Java' started by Matthias Kaeppler, Jun 16, 2005.

  1. 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
     
    Matthias Kaeppler, Jun 16, 2005
    #1
    1. Advertising

  2. 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/computer-lang/java/gui/faq
     
    Thomas Weidenfeller, Jun 16, 2005
    #2
    1. Advertising

  3. Matthias Kaeppler

    Guest

    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/
     
    , Jun 16, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Midnight Java Junkie

    future of high-speed cable modems?

    Midnight Java Junkie, Jun 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    479
    Midnight Java Junkie
    Jun 20, 2004
  2. Elias Politakis

    Is ASP.NET the future of web applications

    Elias Politakis, Dec 1, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,341
    Cowboy \(Gregory A. Beamer\)
    Dec 1, 2003
  3. Elias Politakis

    Is ASP.NET the future of web applications

    Elias Politakis, Dec 1, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    469
  4. =?Utf-8?B?QW5kcmV3?=

    Future of ASP

    =?Utf-8?B?QW5kcmV3?=, Apr 29, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    412
  5. James P.
    Replies:
    7
    Views:
    12,689
    phamtasmic
    Jul 13, 2004
Loading...

Share This Page