1.8.1suspend与resume方法使用
2024-08-28 16:51:39
暂停线程意味着线程还能恢复运行
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
结果分析
线程确实被暂停了,而且还可以恢复成运行的状态。
最新文章
- B树详解
- R入门<;二>;-时间序列研究
- delphi treeview 鼠标移动显示hint信息
- union all合并记录
- ByteBuffer常用方法详解
- struts之拦截器
- windows环境自动获取SVN仓库当前版本
- 再来写一个随机数解决方案,对Random再来一次封装
- sublime text如何保存为uft-8无bom编码格式文件
- Glog使用记录
- linux btrfs文件系统管理与应用
- A Bayesian Approach to Deep Neural Network Adaptation with Applications to Robust Automatic Speech Recognition
- WordPress中默认文本编辑器替换成百度UEditor编辑器
- 小程序点击按钮清空input
- java 调用windows的COM组件举例(使用JACOB)
- 【译】.NET Core 2.2 Preview 2 发布
- TOJ3448: 小学生的作业
- vba遗传算法之非一致性突变
- JS中图片飞飞效果
- 原生js实现拖动滑块验证