suspend()和resume()方法,从字面意义上可以了解到这两个方法是一对的,suspend()方法就是将一个线程挂起(暂停),resume()方法就是将一个挂起线程复活继续执行。参照例子:
package nc.com.thread.traditional.example;
public class TestSuspendAndResume {
public static void main(String[] args) throws Exception {
DemoThread t = new DemoThread();
t.start();
Thread.sleep(1000);
//让线程挂起
t.suspend();
Thread.sleep(1000);
//回复线程,让线程继续执行
t.resume();
System.out.println("-----------------");
}
static class DemoThread extends Thread{
@Override
public void run() {
try {
while(true){
if(Thread.currentThread().isAlive()){
System.out.println("线程"+Thread.currentThread().getName()+"进入就绪状态");
}
}
} catch (Exception e) {
// TODO: handle exception
}
System.out.println("线程"+Thread.currentThread().getName()+"就绪结束");
}
}
}