1. 程式人生 > 實用技巧 >執行緒的五大狀態和測試

執行緒的五大狀態和測試

執行緒的五大狀態分別為:

  • 新建狀態(New): 當用new操作符建立一個執行緒時, 例如new Thread(r),執行緒還沒有開始執行,此時執行緒處在新建狀態。 ...

  • 就緒狀態(Runnable) ...

  • 執行狀態(Running) ...

  • 阻塞狀態(Blocked) ...

  • 死亡狀態(Dead)

執行緒休眠,sleep

package com.cl.state;


//模擬網路延時,作用:放大問題發生性
public class TestSleep implements Runnable{
    //票數
    private  int tickNums=10;

    @Override
    
public void run() { while (true){ if (tickNums<=0){ break; } //模擬延時 try{ Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()
+"拿到了第"+tickNums--+"票"); } } public static void main(String[] args) { TestSleep ticket = new TestSleep(); new Thread(ticket,"king").start(); new Thread(ticket,"king2").start(); new Thread(ticket,"king3").start(); } }

練習:利用sleep模擬倒計時和實時獲取系統當前時間

package
com.cl.state; import java.text.SimpleDateFormat; import java.util.Date; //模擬倒計時 public class TestSleep2 { public static void main(String[] args) { //列印當前系統時間 Date startTime=new Date(System.currentTimeMillis());//獲取系統當前時間 while(true){ try { Thread.sleep(1000); System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime)); startTime=new Date(System.currentTimeMillis());//更新當前時間 } catch (InterruptedException e) { e.printStackTrace(); } } } //模擬倒計時 public static void tenDown() throws InterruptedException{ int num=10; while (true){ Thread.sleep(100); System.out.println(num--); if (num<=0){ break; } } } } //模擬倒計時的main /* * tenDown();直接呼叫
/

執行緒停止,stop

package com.cl.state;

//執行緒停止
//測試stop
//1.建議執行緒正常停止---->利用次數,不建議死迴圈
//2.建議使用標誌位--->設定一個標誌位
//3.不要使用stop或者destory等過時或者jdk不建議使用的方法

public class TestStop implements Runnable {

    //1.設定一個標誌位
    private boolean flag=true;

    @Override
    public void run() {
        int i=0;
        while (flag){
            System.out.println("run....Thread"+i++);
        }
    }

    //2.設定一個公開的方法停止執行緒,轉換標誌位
    public void stop(){
        this.flag=false;
    }


    public static void main(String[] args) {
        TestStop testStop = new TestStop();

        new Thread(testStop).start();

        for (int i = 0; i < 1000; i++) {
            System.out.println("main"+i);
            if (i==900){
                //呼叫stop方法切換標誌位,讓執行緒停止
                testStop.stop();
                System.out.println("執行緒該停止了");
            }
        }
    }
}

執行緒禮讓,yield

package com.cl.state;

//測試禮讓執行緒
//禮讓不一定成功,看cpu心情
public class TestYield {

    public static void main(String[] args) {
        MyYield myYield = new MyYield();

        new Thread(myYield,"a").start();
        new Thread(myYield,"b").start();
    }

}

class MyYield implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"執行緒開始執行");
        Thread.yield();//禮讓
        System.out.println(Thread.currentThread().getName()+"執行緒停止執行");

    }
}

執行緒-強制執行,jion

package com.cl.state;

//測試join方法,強制執行
public class TestJoin implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("執行緒vip來了"+i);
        }
    }

    public static void main(String[] args) throws InterruptedException {

        //啟動主執行緒
        TestJoin testJoin = new TestJoin();
        Thread thread = new Thread(testJoin);
        thread.start();

        //主執行緒

        for (int i = 0; i < 1000; i++) {
            if (i==200){
                thread.join();//插隊
            }
            System.out.println("main"+i);
        }
    }
}

使用者執行緒(eg:mian),守護執行緒(eg:gc,垃圾回收)

package com.cl.state;


//測試守護執行緒
public class TestDaemon {


    public static void main(String[] args) {
        God god = new God();
        You you = new You();

        Thread thread = new Thread();
        thread.setDaemon(true);//預設是false表示使用者執行緒

        thread.start();//守護執行緒

        new Thread(you).start();//使用者執行緒
    }

}

//守護
class God implements Runnable{
    @Override
    public void run() {
        while (true){
            System.out.println("守護著你");
        }
    }
}

//被守護
class You implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 30000; i++) {
            System.out.println("開心的活著");

        }
        System.out.println("hello world");
    }
}