Threading class implementing alternatives to the deprecated suspend and resume methods


Picture:Clock Published on July 17th, 2006 in Java

Finally I came across a project that made heavy use of multithreading. As I wanted to pause and resume one particular thread, I had to use synchronized variables instead of the deprecated suspend and resume methods. I implemented them into my own, clean MyThread class. But since those two, deprecated methods were defined as final methods within the Java framework, I couldn’t override them. So I had to choose other names for them. I went with “pause” and “walk”, pause being pretty self-explanatory. Walk, well uhm, all other words symbolizing movement were unavailable (like continue, run, resume and so on). I know it’s a bad one, but right now, I can’t think of another word symbolizing that meaning, so it’ll work for now. If anyone should have some suggestions, I’m open to renaming that method. I hope this code makes it easier for a few people here, since it makes working with threads a little easier. It’s supposed to provide a base for your threading needs, meaning you can and should extend and modify it to suit your needs. So here’s finally the code:

Update: Thanks to Chad Moore’s suggestion, I changed the name of the “walk” method to proceed.

public class MyThread extends Thread {
    private boolean paused = false;

    public void run() {
         while (true) {
              synchronized (this) {
                   while (paused) {
                        try {
                             //MyThread is waiting
                             wait();
                        } catch (InterruptedException e) {
                             e.printStackTrace();
                        }
                   }
              }
    //MyThread is running
    //Do work
    }
}

    public void pause() {
         synchronized (this) {
              paused = true;
         }
    }

    public void proceed() {
         synchronized (this) {
              paused = false;
              this.notify();
         }
    }
}

And here's a little class for testing purposes:

public class MyThread_Test {
    public static void main(String[] args) throws InterruptedException {
        MyThread mythread = new MyThread();
        System.out.print("Starting MyThread");
        mythread.start();
        Thread.sleep(4000L);
        System.out.println("Stopping MyThread");
        mythread.pause();
        Thread.sleep(4000L);
        System.out.print("Resuming MyThread");
        mythread.proceed();
    }
}
Share This

Trackback URL for this post:
http://www.yatblog.com/2006/07/17/clean-threading-class/trackback/


6 comments

    Chad Moore July 18th, 2006

    How about “proceed” for a name?

    Martin July 18th, 2006

    Woah, thanks! That sounds good. I wanted to take a look at a dictionary or something like that to see what words would match, but I haven’t managed to do so, yet. But “proceed” just sounds nice, I think I’ll go with that one. I’ll repost the new code reflecting the name change soon.

    naisioxerloro November 28th, 2007

    Hi.
    Good design, who make it?

    learn guitar October 16th, 2008

    Learn Guitar
    Learn Guitar

    laniangovaBon October 18th, 2008

    U Will Never Seen This kind Of Joke Before Please Visit hg-palladium.com

    fusRobgluff October 21st, 2008

    Hello! side effects from aldactone

Shout it out!