Threading class implementing alternatives to the deprecated suspend and resume methods
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/
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?
sluctuallut December 18th, 2008
Hi people
As newly registered user i just want to say hi to everyone else who uses this site
JarAnineDen March 20th, 2009
very intresting
sopmepepelp April 22nd, 2009
Hey Everbody
I just joined this forum.
Great work by the admin, mods and seriously every member around.
Just recently I read that there is a cure for diabetes on http://www.healthcaredaily.org
Can diabetes seriously be cured? The source looks like a reliable healthcare news website
Could you someone tell me if this healthcare information is for real?
Thanks a lot
sopmepepelp
StenWeepAler November 22nd, 2009
How to start an online dating site, Adult dating free online service, Dating free internet jewish single site:
dating gravelly,
dating burkittsville,
dating gay new site.
Dating rural hall. Dating north lindenhurst. Dating okreek.