What you want to do - implementing a stop()
method inside a Runnable
- may actually introduce some confusion in the structure of your code. Why? A Runnable
object is, by design, something which holds the code that has to run. But adding a stop()
method would go beyond the sole responsibility of a Runnable
object: that is, you will make it able to control the execution of your code. That should be avoided.
Here is a hint: there is no start()
method in Runnable
. Indeed, there is one in the Thread
class, and the Runnable
interface has been introduced in the JDK to reduce coupling between the "runnable" code and the object which controls its execution (an instance of Thread
).
My question is: why aren't you satisified with the future.cancel()
? Could you please add some precisions in your design requirements?