JAVA多線程優先級設置與獲取

線程的優先級指的是,線程的優先級越高越有可能先執行,但僅僅是有可能而已。

在Thread類中提供有如下優先級方法:

  • 設置優先級
public final void setPriority(int newPriority)
  • 獲取優先級
public final int getPriority()

對於優先級設置的內容可以通過Thread類的幾個常量來決定:

  1. 最高優先級:public final static int MAX_PRIORITY = 10;
  2. 中等優先級:public final static int NORM_PRIORITY = 5;
  3. 最低優先級:public final static int MIN_PRIORITY = 1;

設置優先級

public class MyRunnable implements Runnable  {
    public void run() {
        int i=3;
        while(i>0){
            System.out.println(Thread.currentThread().getName()+":"+i);
            i--;
        }
    }
    public static void main(String[] args) throws InterruptedException {
        MyRunnable myThread=new MyRunnable();
        Thread thread1=new Thread(myThread,"線程1");
        Thread thread2=new Thread(myThread,"線程2");
        Thread thread3=new Thread(myThread,"線程3");
        thread1.setPriority(Thread.MIN_PRIORITY);
        thread2.setPriority(Thread.MIN_PRIORITY);
        thread3.setPriority(Thread.MAX_PRIORITY);
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

在這裏插入圖片描述
那麼主方法也是一個線程,主方法的優先級是什麼呢?

  public static void main(String[] args) throws InterruptedException {
      int a= Thread.currentThread().getPriority();
        System.out.println(a);
    }

在這裏插入圖片描述

線程具有繼承性

比如在線程A中啓動了線程B,那麼線程A和線程B的優先級將是一樣的,代碼如下:

class A implements Runnable{

    @Override
    public void run() {
        System.out.println("A的優先級是"+Thread.currentThread().getPriority());
        Thread thread=new Thread(new B());
        thread.start();
    }
}
class B implements Runnable{

    @Override
    public void run() {
        System.out.println("B的優先級是"+Thread.currentThread().getPriority());
    }
}
public class Test3 {
    public static void main(String[] args) {
        Thread thread=new Thread(new A());
        thread.setPriority(Thread.MAX_PRIORITY);
        thread.start();
    }
}

在這裏插入圖片描述

守護線程

守護線程是一種特殊的線程,它屬於是一種陪伴線程。簡單點說 java 中有兩種線程:用戶線程和守護線程。可以通過 isDaemon() 方法來區別它們:如果返回 false ,則說明該線程是“用戶線程”;否則就是“守護線程”。典型的守護線程就是垃圾回收線程。只要當前JVM進程中存在任何一個非守護線程沒有結束,守護線程就在工作;只有當最後一個非守護線程結束時,守護線程纔會隨着JVM一同停止工作。

注意:主線程main是用戶線程。

public class MyRunnable implements Runnable  {
    private int i=1;
    public void run() {
            try {
        while(true){
            i++;
            System.out.println("線程名稱:"+Thread.currentThread().getName()+" i="+i+" 是否是守護線程:"+Thread.currentThread().isDaemon());
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
                System.out.println(Thread.currentThread().getName()+"中斷了");
        }
    }
    public static void main(String[] args) throws InterruptedException {
        MyRunnable myRunnable=new MyRunnable();
      Thread thread=new Thread(myRunnable,"線程A");
      thread.setDaemon(true);
      thread.start();
      Thread thread1=new Thread(myRunnable,"線程B");
      thread1.start();
      Thread.sleep(3000);
      thread1.interrupted();
      Thread.sleep(1000);
        System.out.println("程序結束");
    }
}

結果表明,線程B中斷後,線程A還沒有結束,那是因爲主線程還沒有結束,所以 守護線程不會結束。說明用戶線程結束之後守護線程纔會結束。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章