Comments on: Cancelor – a java task cancelation service http://v1.ripper234.com/p/cancelor-a-java-task-cancelation-servic/ Stuff Ron Gross Finds Interesting Sun, 02 Aug 2015 11:03:35 +0000 hourly 1 https://wordpress.org/?v=4.5.3 By: ripper234 http://v1.ripper234.com/p/cancelor-a-java-task-cancelation-servic/comment-page-1/#comment-1976 Fri, 15 Jan 2010 14:35:32 +0000 http://v1.ripper234.com/?p=1217#comment-1976 Strike that, Future is an interface, not a class. You’ll have to modify the code of the particular ExecutorService you’re using to return a Future that supports this, I think … does’t sound too nice.

]]>
By: ripper234 http://v1.ripper234.com/p/cancelor-a-java-task-cancelation-servic/comment-page-1/#comment-1975 Fri, 15 Jan 2010 14:34:25 +0000 http://v1.ripper234.com/?p=1217#comment-1975 Fair enough. Alright, one more question: How would you use this if you’re not starting the thread directly but rather using an ExecutorService? You only get a Future back, I don’t believe you have direct access to the thread. I haven’t look at the code of Future yet, but you could probably extend Future to support this.

]]>
By: Tomer Gabel http://v1.ripper234.com/p/cancelor-a-java-task-cancelation-servic/comment-page-1/#comment-1967 Wed, 13 Jan 2010 13:37:19 +0000 http://v1.ripper234.com/?p=1217#comment-1967 What’s IoC got to do with it? You can always inject an “ICancellationService” and expose the threadlocal as a getToken() method call or whatever. Using InheritableThreadLocal serves you the bother of having to manually route the flag between child tasks.

]]>
By: ripper234 http://v1.ripper234.com/p/cancelor-a-java-task-cancelation-servic/comment-page-1/#comment-1966 Wed, 13 Jan 2010 13:23:30 +0000 http://v1.ripper234.com/?p=1217#comment-1966 Tomer, a boolean or AtomicBoolean is indeed the second other solution I discussed. The one downside is that sometimes you need to pass this boolean along a few call chains, which doesn’t play nice with IOC frameworks like Guice. It is true however that it’s a KISS solution that just works.

]]>
By: Tomer Gabel http://v1.ripper234.com/p/cancelor-a-java-task-cancelation-servic/comment-page-1/#comment-1964 Wed, 13 Jan 2010 11:45:14 +0000 http://v1.ripper234.com/?p=1217#comment-1964 Instead of using a string value and searching a global hashmap (potentially inefficient), there are several things you can do:

* Ideally you’d use a volatile bool for this (since there’s only one state change — cancelled: false->true). I’m not sure it’s possible to pass such a variable byref to child threads, so you’ll probably need a reference type in there anyway.
* Since that’s the case, you may want to use a single flag variable (AtomicBoolean), place it in an InheritableThreadLocal (http://java.sun.com/javase/6/docs/api/java/lang/InheritableThreadLocal.html) and expose it once when the primary task is constructed. This allows you to cancel the entire task tree with a single boolean write. I’m not sure how well this performs, but it does seem like an elegant solution to the problem and possibly worth investigating. There may also be issues when using a scheduler (since all tasks share a common thread pool) but I suppose there are ways around that as well.

]]>