暂停线程意味着线程还能恢复运行
suspend()方法暂停线程。resume()恢复线程
测试如下
package com.cky.thread; /**
* Created by edison on 2017/12/3.
*/
public class MyThread extends Thread{
private long i=0;
public long getI() {
return i;
} public void setI(long i) {
this.i = i;
} @Override
public void run() {
super.run();
while(true) {
i++;
}
}
}
package com.cky.test; import com.cky.thread.MyThread; /**
* Created by edison on 2017/12/3.
*/
public class Test {
public static void main(String[] args) {
try {
MyThread myThread = new MyThread();
myThread.start();
Thread.sleep(5000); //A断
myThread.suspend();
System.out.println("A= "+ System.currentTimeMillis()+ " i="+myThread.getI());
Thread.sleep(5000);
System.out.println("A= "+ System.currentTimeMillis()+ " i="+myThread.getI()); //B段
myThread.resume();
Thread.sleep(5000); //c断
myThread.suspend();
System.out.println("B= "+ System.currentTimeMillis()+ " i="+myThread.getI());
Thread.sleep(5000);
System.out.println("B= "+ System.currentTimeMillis()+ " i="+myThread.getI());
} catch (InterruptedException e) {
e.printStackTrace();
} }
}
C:\itsoft\jdk\bin\java -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:\itsoft\idea\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\itsoft\jdk\jre\lib\charsets.jar;C:\itsoft\jdk\jre\lib\deploy.jar;C:\itsoft\jdk\jre\lib\ext\access-bridge-32.jar;C:\itsoft\jdk\jre\lib\ext\cldrdata.jar;C:\itsoft\jdk\jre\lib\ext\dnsns.jar;C:\itsoft\jdk\jre\lib\ext\jaccess.jar;C:\itsoft\jdk\jre\lib\ext\jfxrt.jar;C:\itsoft\jdk\jre\lib\ext\localedata.jar;C:\itsoft\jdk\jre\lib\ext\nashorn.jar;C:\itsoft\jdk\jre\lib\ext\sunec.jar;C:\itsoft\jdk\jre\lib\ext\sunjce_provider.jar;C:\itsoft\jdk\jre\lib\ext\sunmscapi.jar;C:\itsoft\jdk\jre\lib\ext\sunpkcs11.jar;C:\itsoft\jdk\jre\lib\ext\zipfs.jar;C:\itsoft\jdk\jre\lib\javaws.jar;C:\itsoft\jdk\jre\lib\jce.jar;C:\itsoft\jdk\jre\lib\jfr.jar;C:\itsoft\jdk\jre\lib\jfxswt.jar;C:\itsoft\jdk\jre\lib\jsse.jar;C:\itsoft\jdk\jre\lib\management-agent.jar;C:\itsoft\jdk\jre\lib\plugin.jar;C:\itsoft\jdk\jre\lib\resources.jar;C:\itsoft\jdk\jre\lib\rt.jar;C:\多线程核心技术\第一章\out\production\第一章;C:\itsoft\idea\IntelliJ IDEA 2016.3.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test
A= 1512288685100 i=3391077327
A= 1512288690111 i=3391077327
B= 1512288695126 i=6819461694
B= 1512288700129 i=6819461694
结果分析
线程确实被暂停了,而且还可以恢复成运行的状态。