Cancel or Pause a Thread’s execution – the basics.

I’ve thought about this wishing to be able to stop some processes while running on some timers, and to stop them safely and clean. For instance a process would download some large amount of data, then analyze it, maybe store it, then send some mails or notifications. Basically a complex list of things that might take more the few seconds to run, up to minutes or more.

How can you safely stop a process like this? I’m sure that even if you didn’t do it so far and think a bit you might very well find solutions.

Basically, in what I did, there is only one simple trick. The idea is to constantly call a method that will throw and exception if the status is CANCEL. In more details, implement the Runnable, implement run() as final and call from there an abstract method where the actual implementation of the process will be i.e. execute(). Then in your implementation, anyplace where you can afford to stop the execution make calls to checkInterruption(), which will throw an exception in case the process was canceled, or if paused it will do a wait() until resumed.

I found this solution as being really easy to implement and use, safely enough, clean, reusable, scalable.

You can find it here as a Java implementation: https://github.com/bogdanudrescu/SimpleThreadsAPI

The API is straight forward: https://github.com/bogdanudrescu/SimpleThreadsAPI/blob/master/src/grape/simple/threads/InterruptibleRunnable.java

public interface InterruptibleRunnable extends Runnable {

	void cancel();

	void pause();

	void resume();

	public InterruptibleRunnableState getState();

	public InterruptibleRunnableState getRealState();

	public void addPropertyChangeListener(PropertyChangeListener listener);

	public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener);

	public float getPercentCompleted();
}

https://github.com/bogdanudrescu/SimpleThreadsAPI/blob/master/src/grape/simple/threads/InterruptibleRunnableState.java

public enum InterruptibleRunnableState {

	NOT_RUNNING,

	RUNNING,

	PAUSED,

	CANCELED
}

OK, the actual code looks more beautiful, but to save some space here I remove the comments. 🙂

Quick look on the actual class implementation: https://github.com/bogdanudrescu/SimpleThreadsAPI/blob/master/src/grape/simple/threads/AbstractInterruptibleRunnable.java

It also contains a very simple test sample on how to use it.

For a bit more complex example please read the next article: Simple Threads – a bit more complex approach

I called my package grape simple because my wife really love grapes. We have the custom each year to make wine, which we froze it on the spot. We can thus open the bottles at any time and it will be really sweet and with no alcohol.

Autumn Grape

Advertisements

6 responses to “Cancel or Pause a Thread’s execution – the basics.

  1. Very nice explanation in the cancel and pause of thread execution. I just finished the basic of Java Threads from the book Head First Java and your explanation of cancel and pause was excellent.

    • I’m glad you find it interesting. And I really hope this approach will be helpful in your projects, as it was in mine.
      And, btw, good job with the Head First Java book and blogging from it. That’s the best learning book who knows how to motivate.

      • Truly, Head First Java is a saviour for people like me who are starting in Java, And once the basic are clear, i can understand the concept you have implement in your blog. gr8 Help. Keep blogging

      • You know, when I started this blog I did it with something in mind, to present a different approach in dealing with software, involving both technology and art in the same time, because after all, we’re all humans. Then I saw that here, on WordPress, people talk more about lifestyle or general interests, and I thought I might do it wrong. But you really motivate me to go on. Thank you!

      • Your style of writing is very different, since you try to mix art with programming, So it is enjoyable. I liked the idea of you having grapes in you package name.
        It was really something different, In our day job we are so much bogged down with Process and Coding GuideLines etc. We forget that Programming is actually an ART.
        The best book on Algorithm is actually called “The Art of Programming” by Knuth.
        Keep Blogging, i guarantee i will be reading your post.

  2. Pingback: Simple Threads – a bit more complex approach | Develop with love

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s