線程狀態(tài)/休眠/讓步/插隊/優(yōu)先級/守護線程

線程狀態(tài)

  • 創(chuàng)建狀態(tài)
  • 就緒狀態(tài)
  • 阻塞狀態(tài)
  • 運行狀態(tài)
  • 死亡狀態(tài)
[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-i5xJhuPZ-1592308713404)(C:\Users\車澤平\AppData\Roaming\Typora\typora-user-images\1592285267192.png)]
在這里插入圖片描述

線程方法

在這里插入圖片描述

停止線程

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-w0gzJZok-1592308713424)(C:\Users\車澤平\AppData\Roaming\Typora\typora-user-images\1592285523432.png)]

線程休眠 sleep()

  • sleep(毫秒數(shù))指定當前線程停止的實踐
  • sleep()存在異常InteruptedException
  • sleep()實踐到達后線程進入就緒狀態(tài)
  • sleep()可以模擬網(wǎng)絡(luò)延時,倒計時等
  • 每一個對象都有一個鎖,sleep不會釋放鎖

線程禮讓 yield()

  • 禮讓線程,讓當前正在執(zhí)行的線程暫停,但不阻塞
  • 將線程從運行狀態(tài)轉(zhuǎn)為就緒狀態(tài)
  • 讓CPU重新調(diào)度,禮讓不一定成功,看CPU心情

線程強制執(zhí)行 join()

  • Join合并線程,待此線程執(zhí)行完成后,在執(zhí)行其他線程,其他線程阻塞
  • 可以想象成插隊
public class TestJoin  implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 5000; 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 == 20){
                thread.join();
            }
            System.out.println("普通"+i);
        }
    }
}

線程狀態(tài)

public class TestState {

    public static void main(String[] args) throws InterruptedException {
     Thread thread = new Thread(()->{
         for (int i = 0; i < 5; i++) {
             try {
                 Thread.sleep(1000);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         }
         System.out.println("http:///////////");
        });
        Thread.State state = thread.getState();
        System.out.println(state);

        thread.start();
         state = thread.getState();
        System.out.println(state);

        while (state != Thread.State.TERMINATED){
            Thread.sleep(500);
            state = thread.getState();
            System.out.println(state);
        }
    }
}

線程優(yōu)先級

java提供一個線程調(diào)度器來監(jiān)控程序中啟動后進入就緒狀態(tài)的所有線程;線程調(diào)度器按照優(yōu)先級決定該調(diào)度哪個線程來執(zhí)行
線程的優(yōu)先級用數(shù)字來表示,范圍從1~10
Thread.MIN_PRIORITY = 1
Thread.MAX_PRIORITY= 10
Thread.NORM_PRIORITY = 5
使用getPriority()和setPriority()來獲取或改變優(yōu)先級

優(yōu)先級低只是意味著獲得調(diào)度的概率低,并不是優(yōu)先級低就不會被調(diào)用了,這都是看CPU的調(diào)度。(性能倒置)
public class TestPriority {
    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName()+"-----"+Thread.currentThread().getPriority());

        MyPriority myPriority = new MyPriority();
        Thread thread = new Thread(myPriority);
        thread.setPriority(10);
        thread.start();
    }
}
class MyPriority implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"-----"+Thread.currentThread().getPriority());
    }
}

守護線程setDeamon()

線程分為用戶線程和守護線程
虛擬機必須確保用戶線程執(zhí)行完畢(main)
虛擬機不用等待守護線程執(zhí)行完畢(GC)
如后臺記錄操作日志,監(jiān)控內(nèi)存,垃圾回收

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容