1.8.1挂起线程(暂停线程)

来源:互联网 发布:软件维护是指 编辑:程序博客网 时间:2024/06/11 02:00

package demo;/** * Created by sunyifeng on 17/10/10. */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(){        while (true) {            i++;        }    }}
package demo;/** * Created by sunyifeng on 17/10/10. */public class Run {    public static void main(String[] args) {        try {            MyThread myThread = new MyThread();            myThread.start();            Thread.sleep(5000);            // 挂起            myThread.suspend();            System.out.println("A=" + System.currentTimeMillis() + ",i=" + myThread.getI());            Thread.sleep(5000);            System.out.println("A=" + System.currentTimeMillis() + ",i=" + myThread.getI());            // 唤醒            myThread.resume();            Thread.sleep(5000);            // 挂起            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();        }    }}
运行结果:

A=1507571201939,i=2244120410
A=1507571206943,i=2244120410
B=1507571211946,i=4650198636
B=1507571216950,i=4650198636