成功的路上总是离不开贵人的帮助,名师的指点和小人的刺激。

莫怕,过了桥,就翻篇了

1.8.1suspend与resume方法使用

暂停线程意味着线程还能恢复运行

suspend()方法暂停线程。resume()恢复线程

测试如下

 1 package com.cky.thread;
 2 
 3 /**
 4  * Created by edison on 2017/12/3.
 5  */
 6 public class MyThread extends Thread{
 7     private long i=0;
 8     public long getI() {
 9         return i;
10     }
11 
12     public void setI(long i) {
13         this.i = i;
14     }
15 
16     @Override
17     public void run() {
18         super.run();
19         while(true) {
20             i++;
21         }
22     }
23 }
 1 package com.cky.test;
 2 
 3 import com.cky.thread.MyThread;
 4 
 5 /**
 6  * Created by edison on 2017/12/3.
 7  */
 8 public class Test {
 9     public static void main(String[] args) {
10         try {
11             MyThread myThread = new MyThread();
12             myThread.start();
13             Thread.sleep(5000);
14 
15             //A断
16             myThread.suspend();
17             System.out.println("A= "+ System.currentTimeMillis()+ " i="+myThread.getI());
18             Thread.sleep(5000);
19             System.out.println("A= "+ System.currentTimeMillis()+ " i="+myThread.getI());
20 
21             //B段
22             myThread.resume();
23             Thread.sleep(5000);
24 
25             //c断
26             myThread.suspend();
27             System.out.println("B= "+ System.currentTimeMillis()+ " i="+myThread.getI());
28             Thread.sleep(5000);
29             System.out.println("B= "+ System.currentTimeMillis()+ " i="+myThread.getI());
30         } catch (InterruptedException e) {
31             e.printStackTrace();
32         }
33 
34     }
35 }
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

结果分析

线程确实被暂停了,而且还可以恢复成运行的状态。

posted on 2017-12-03 16:14  痞子陈2016  阅读(276)  评论(0编辑  收藏  举报

导航