The methods Thread.resume(), Thread.suspend() and Thread.stop() are deprecated. Now you can implement your own methods using wait() , notify() and a state flag.
This is my code example:
public enum State { ACTIVE,SUSPENDED,STOPPED; } public class MyThread implements Runnable { private State currState; public MyThread() { this.currState = State.ACTIVE; } @Override public void run() { try { while(currState != State.STOPPED) { Thread.sleep(1000); synchronized (this) { while(currState == State.SUSPENDED) this.wait(); // Here I'm suspended // Here I'm released } } // Here I'm stopped } catch (InterruptedException e) { e.printStackTrace(); // Set interrupt state again, as the thread has no interruption policy Thread.currentThread().interrupt(); } } synchronized void suspendThread() { this.currState = State.SUSPENDED; } synchronized void resumeThread() { this.currState = State.ACTIVE; this.notify(); } synchronized void stopThread() { this.currState = State.STOPPED; this.notify(); } }
Copyright © 2013 Welcome to the website of Davis Fiore. All Rights Reserved.