• Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic question: how to stop a Thread with a Future (java 5)  RSS feed

 
Wolf Benz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have 2 basic questions I'd like to drop here, in hope of a more Concurrency-senior member being able to help me out here.

Have I understood the purpose of "Futures" correct in that it does indeed allow you to set intermediate results of a Thread in such Object, but that is this the responsibility of the developper?
If so, am I right in concluding that calling "FutureObject.cancel()" won't stop the Thread by itself unless you code it yourself in the implementation of that cancel method? ( -> How do you get from the Future to the Thread it shadows?!)

Thanks for advice,
Wolf
 
Henry Wong
author
Sheriff
Posts: 22814
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If so, am I right in concluding that calling "FutureObject.cancel()" won't stop the Thread by itself unless you code it yourself in the implementation of that cancel method?


Calling cancel() will try to remove the task from the executor, if it has not been started. If it has been started, and is still running, then you have the option to send an interrupt to the task -- which hopefully, you have coded to recieve, stop what it was doing, and clean up after itself.

-> How do you get from the Future to the Thread it shadows?!


You don't want the thread stopped. The thread belongs to the executor, and needs to be used for other tasks in the executor.

You merely want your task stopped -- to stop what it is doing and exit (your runnable or callable object). This can be done by dealing with interrupts. Or you can implement your own flags -- the callable object is your object, so you can set it up with flags or methods to terminate early too.

Henry
 
Wolf Benz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry,
Thanks for your replies.
Can I conclude from your post that we as developers don't have to bother what Future is coupled with what task, and that it is the JVM keeping track of this?
So when I have
FutureObject = Executor.submit(CallableInstance);
... I don't have to explecitely implement FutureObject's cancel()?

-Wolf
 
Henry Wong
author
Sheriff
Posts: 22814
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The future object's cancel method is for the executor. If you want to be able to cancel the task, after it has been started, you will have to implement that yourself. And this is separate from cancelling from the executor.

Henry
 
Wolf Benz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see.
And how do I get from within the FutureObject's cancel() method to the Thread I wish to interrupt?
(I don't know what Future is coupled with what Thread do I?)

-Wolf
 
Henry Wong
author
Sheriff
Posts: 22814
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Wolf Benz:
I see.
And how do I get from within the FutureObject's cancel() method to the Thread I wish to interrupt?
(I don't know what Future is coupled with what Thread do I?)

-Wolf


If you want to send an interrupt to the thread running your task, there is an option to do so, as part of the cancel() method.... but keep in mind, it will only be sent if the task is actually running.

Henry
 
Wolf Benz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how can you do that?
Will (within in FutureObject's cancel() method))

work(assuming Sun implemented this with ThreadLocals), or is there a better way?

-Wolf
 
Henry Wong
author
Sheriff
Posts: 22814
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will (within in FutureObject's cancel() method))


What do you mean by "within in FutureObject's cancel() method"?


Just call your future object's cancel method, with the interrupt flag -- and if it is running, your task will get the interrupt.

fo.cancel(true);

Henry
[ November 26, 2008: Message edited by: Henry Wong ]
 
Wolf Benz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "within in FutureObject's cancel() method"?

-> Well: you told me I had to implement FutureObject's cancel() method to interrupt the Thread.
So I asked: how do you implement it?
= How do you code that cancel method to cancel the Thread that the FutureObject is associated with?
(my first attempt would then be the Thread.currentThread.interrupt())

Seems there is a misunderstanding somewhere, w.r.t. my question.
Apologies.
-Wolf
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!