> Hello,

> Sometimes, one wants to answer to an event by starting a
> (potentially long) computation in the background.
> But if the same event is received again, one may want to stop the
> computation currently running in the background, and relaunch one in the 
> background with newest data.

> Is this use case a job for the 'future function, or would you do
> that with other clojure constructs ?

> To make it more concrete, what I have in mind is something along
> the lines of the Eclipse Jobs API, but done with clojure.

> Thanks in advance for your ideas,

Yeah, futures are a good way to deal with cancellable tasks.  You can
call (.cancel my-future true), to cancel the task using thread
interruption.  See:
<http://java.sun.com/javase/6/docs/api/java/util/concurrent/Future.html#cancel(boolean)>

In order to support this your task will need to support the
interruption 'protocol' by handling InterruptedExceptions correctly,
and polling for interruption as appropriate.

Thread interruption doesn't seem to be typically well understood by
Java programmers. I'd suggest reading Chapter 7 of the excellent Java
Concurrency In Practice. <http://jcip.net/>

Futures simplify interruption in Java because they ensure that the
task you are trying to cancel is the one that gets cancelled, even if
the task is running in a thread pool and you happen to issue your
cancellation after the task has completed and another task has started
running on that thread.


-- 
Dave



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to