天天看點

suspend與resume使用

暫停和恢複線程

suspend 用于暫停線程

resume 用于恢複線程

public class Test161 {
    public static void main(String[] args) {
    MyThread1 m = new MyThread1();
    Thread t = new Thread(m);
    t.start();
    try {
        Thread.sleep();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    t.suspend();
    System.out.println(System.currentTimeMillis()+" "+m.getI());
   //這裡設睡眠是充分讓線程擷取cpu執行權    
    try {
        Thread.sleep();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println(System.currentTimeMillis()+" "+m.getI());

    t.resume();
    try {
        Thread.sleep();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println(System.currentTimeMillis()+" "+m.getI());




    }
}

class MyThread1 implements Runnable{
    private int i ;



    public int getI() {
        return i;
    }



    public void setI(int i) {
        this.i = i;
    }



    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(true) {
            i++;
        }
    }

}
           

執行結果:

suspend與resume使用

主要看恢複線程,是從線程暫停的位置開始啟動的,恢複原來運作的狀态。

獨占問題

public class Test162 {
    public static void main(String[] args) {
        MyThread66 m = new MyThread66();
        Thread tt1 = new Thread(m);
        Thread tt2 = new Thread(m);
        tt1.setName("a");
        tt2.setName("b");
        tt1.start();
        tt2.start();


    }

     synchronized public void show() {
        if(Thread.currentThread().getName().equals("a")) {
            System.out.println("目前進來的線程暫停了");
            Thread.currentThread().suspend();

        }
    }
}

class MyThread66 implements Runnable{
    Test162 t = new Test162();
    @Override
    public void run() {
        // TODO Auto-generated method stub
        t.show();
    }

}
           

在有對象螢幕的方法中,如果目前運作的線程如果暫定,其他線程是無法通路該方法,因為它沒有釋放鎖。

資料不一緻問題

package com.example.test;

public class Test163 {
    private String name ="xxx";
    private int age=;
    public void setValue(String name,int age) {
        this.name = name;
        if(Thread.currentThread().getName().equals("a")) {
        Thread.currentThread().suspend();
        }
        this.age = age;

    }
    @Override
    public String toString() {
        System.out.println(name+"  "+age);
        return null;
    }

    public static void main(String[] args) {
        MyThread88 m8 =  new MyThread88();
        Thread t2  = new Thread(m8);
        Thread t3  = new Thread(m8);
        t2.setName("a");
        t2.start();
        try {
            Thread.sleep();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        t3.start();
    }
}

class MyThread88 implements Runnable {
    Test163 t = new Test163();

    @Override 
    public void run() {
        // TODO Auto-generated method stub
        if(Thread.currentThread().getName().equals("a")) {
            t.setValue("laoqqiang", );
        }else {
            t.toString();
        }
    }
}
           

我們通過兩個新的線程來操作資料,中間暫停了,另外一個線程在列印資料,就出現不一緻的問題。