【Java】多線程其實可以設置優先級

Java優先級

Java提供一個線程調度器來監視和控制Runnable狀態的線程。線程的調度策略採用搶佔式,優先級高的線程比優先級低的線程優先執行。在優先級相同的情況下,按照“先到先服務”的原則。

每個Java程序都有一個默認的主線程,就是通過JVM啓動的第一個線程。對於應用程序,主線程執行的是main()方法。對於Applet主線程是指瀏覽器加載並執行小應用程序的那一個線程。

子線程是由應用程序創建的線程。

還有一種線程稱爲守護現成(Daemon),這是一種用於監視其他線程工作的服務線程,優先級爲最低。

Thread 類中,使用如下屬性來代表優先級。

private int priority;

我們可以通過 setPriority(int newPriority) 來設置新的優先級,通過 getPriority() 來獲取線程的優先級。

有些資料通過下面的例子就得出了一個結論:Java 線程默認優先級是 5

public static void main(String[] args) {
    Thread thread = new Thread();
    System.out.println(thread.getPriority());
}

 打印結果:

5

其實這是大錯特錯的,只是看到了表面,看看下面的例子,我們把當前線程的優先級改爲 4,發現子線程 thread 的優先級也是 4。

public static void main(String[] args) {
    Thread.currentThread().setPriority(4);
    Thread thread = new Thread();
    System.out.println(thread.getPriority());
}

打印結果:

4

這啪啪啪打臉了,如果是線程默認優先級是 5,我們新創建的 thread 線程,沒設置優先級,理應是 5,但實際是 4。我們看看 Thread 初始化 priority 的源代碼。

Thread parent = currentThread();
this.priority = parent.getPriority();

原來,線程默認的優先級是繼承父線程的優先級,上面例子我們把父線程的優先級設置爲 4,所以導致子線程的優先級也變成 4。

嚴謹一點說,子線程默認優先級和父線程一樣,Java 主線程默認的優先級是 5。

Java 中定義了 3 種優先級,分別是最低優先級(1)正常優先級(5)最高優先級(10),代碼如下所示。Java 優先級範圍是 [1, 10],設置其他數字的優先級都會拋出 IllegalArgumentException 異常。

/**
 * The minimum priority that a thread can have.
 */
public final static int MIN_PRIORITY = 1;

/**
 * The default priority that is assigned to a thread.
 */
public final static int NORM_PRIORITY = 5;

/**
 * The maximum priority that a thread can have.
 */
public final static int MAX_PRIORITY = 10;

接下來說說線程優先級的作用。先看下面代碼,代碼邏輯是創建了 3000 個線程,分別是: 1000 個優先級爲 1 的線程, 1000 個優先級爲 5 的線程,1000 個優先級爲 10 的線程。用 minTimes 來記錄 1000 個 MIN_PRIORITY 線程運行時時間戳之和,用 normTimes 來記錄 1000 個 NORM_PRIORITY 線程運行時時間戳之和,用 maxTimes 來記錄 1000 個 MAX_PRIORITY 線程運行時時間戳之和。通過統計每個優先級的運行的時間戳之和,值越小代表的就是越優先執行。我們運行看看。

public class TestPriority {
    static AtomicLong minTimes = new AtomicLong(0);
    static AtomicLong normTimes = new AtomicLong(0);
    static AtomicLong maxTimes = new AtomicLong(0);

    public static void main(String[] args) {
        List<MyThread> minThreadList = new ArrayList<>();
        List<MyThread> normThreadList = new ArrayList<>();
        List<MyThread> maxThreadList = new ArrayList<>();

        int count = 1000;
        for (int i = 0; i < count; i++) {
            MyThread myThread = new MyThread("min----" + i);
            myThread.setPriority(Thread.MIN_PRIORITY);
            minThreadList.add(myThread);
        }
        for (int i = 0; i < count; i++) {
            MyThread myThread = new MyThread("norm---" + i);
            myThread.setPriority(Thread.NORM_PRIORITY);
            normThreadList.add(myThread);
        }
        for (int i = 0; i < count; i++) {
            MyThread myThread = new MyThread("max----" + i);
            myThread.setPriority(Thread.MAX_PRIORITY);
            maxThreadList.add(myThread);
        }

        for (int i = 0; i < count; i++) {
            maxThreadList.get(i).start();
            normThreadList.get(i).start();
            minThreadList.get(i).start();
        }

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("maxPriority 統計:" + maxTimes.get());
        System.out.println("normPriority 統計:" + normTimes.get());
        System.out.println("minPriority 統計:" + minTimes.get());
        System.out.println("普通優先級與最高優先級相差時間:" + (normTimes.get() - maxTimes.get()) + "ms");
        System.out.println("最低優先級與普通優先級相差時間:" + (minTimes.get() - normTimes.get()) + "ms");

    }

    static class MyThread extends Thread {

        public MyThread(String name) {
            super(name);
        }

        @Override
        public void run() {
            System.out.println(this.getName() + " priority: " + this.getPriority());
            switch (this.getPriority()) {
                case Thread.MAX_PRIORITY :
                    maxTimes.getAndAdd(System.currentTimeMillis());
                    break;
                case Thread.NORM_PRIORITY :
                    normTimes.getAndAdd(System.currentTimeMillis());
                    break;
                case Thread.MIN_PRIORITY :
                    minTimes.getAndAdd(System.currentTimeMillis());
                    break;
                default:
                    break;
            }
        }
    }
}

執行結果如下:

# 第一部分
max----0 priority: 10
norm---0 priority: 5
max----1 priority: 10
max----2 priority: 10
norm---2 priority: 5
min----4 priority: 1
.......
max----899 priority: 10
min----912 priority: 1
min----847 priority: 5
min----883 priority: 1

# 第二部分
maxPriority 統計:1568986695523243
normPriority 統計:1568986695526080
minPriority 統計:1568986695545414
普通優先級與最高優先級相差時間:2837ms
最低優先級與普通優先級相差時間:19334ms

我們一起來分析一下結果。先看看第一部分,最開始執行的線程高優先級、普通優先級、低優先級都有,最後執行的線程也都有各個優先級的,這說明了:優先級高的線程不代表一定比優先級低的線程優先執行。也可以換另一種說法:代碼執行順序跟線程的優先級無關。看看第二部分的結果,我們可以發現最高優先級的 1000 個線程執行時間戳之和最小,而最低優先級的 1000 個線程執行時間戳之和最大,因此可以得知:一批高優先級的線程會比一批低優先級的線程優先執行,即高優先級的線程大概率比低優先的線程優先獲得 CPU 資源

各操作系統中真有 10 個線程等級麼?

Java 作爲跨平臺語言,線程有 10 個等級,但是映射到不同操作系統的線程優先級值不一樣。接下來教大家怎麼在 OpenJDK 源碼中查各個操作系統中線程優先級映射的值。

1.看到 Thread 源代碼,設置線程優先級最終調用了本地方法 setPriority0()

private native void setPriority0(int newPriority);

2.接着我們在 OpenJDK 的 Thread.c 代碼中找到 setPriority0() 對應的方法 JVM_SetThreadPriority

static JNINativeMethod methods[] = {
    ...
    {"setPriority0",     "(I)V",       (void *)&JVM_SetThreadPriority},
    ...
};

3.我們根據 JVM_SetThreadPriority 找到 jvm.cpp 中對應的代碼段;

JVM_ENTRY(void, JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio))
  JVMWrapper("JVM_SetThreadPriority");
  // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
  MutexLocker ml(Threads_lock);
  oop java_thread = JNIHandles::resolve_non_null(jthread);
  java_lang_Thread::set_priority(java_thread, (ThreadPriority)prio);
  JavaThread* thr = java_lang_Thread::thread(java_thread);
  if (thr != NULL) {                  // Thread not yet started; priority pushed down when it is
    Thread::set_priority(thr, (ThreadPriority)prio);
  }
JVM_END

4.根據第 3 步中的代碼,我們可以發現關鍵是 java_lang_Thread::set_Priority() 這段代碼,繼續找 thread.cpp 代碼中的 set_Priority() 方法;

void Thread::set_priority(Thread* thread, ThreadPriority priority) {
  trace("set priority", thread);
  debug_only(check_for_dangling_thread_pointer(thread);)
  // Can return an error!
  (void)os::set_priority(thread, priority);
}

5.發現上面代碼最終調用的是 os::set_priority(),接着繼續找出 os.cpp 的 set_priority() 方法

OSReturn os::set_priority(Thread* thread, ThreadPriority p) {
#ifdef ASSERT
  if (!(!thread->is_Java_thread() ||
         Thread::current() == thread  ||
         Threads_lock->owned_by_self()
         || thread->is_Compiler_thread()
        )) {
    assert(false, "possibility of dangling Thread pointer");
  }
#endif

  if (p >= MinPriority && p <= MaxPriority) {
    int priority = java_to_os_priority[p];
    return set_native_priority(thread, priority);
  } else {
    assert(false, "Should not happen");
    return OS_ERR;
  }
}

6.終於發現了最終轉換爲各操作系統的優先級代碼 java_to_os_priority[p],接下來就是找各個操作系統下的該數組的值。比如下面是 Linux 系統的優先級值。

int os::java_to_os_priority[CriticalPriority + 1] = {
  19,              // 0 Entry should never be used

   4,              // 1 MinPriority
   3,              // 2
   2,              // 3

   1,              // 4
   0,              // 5 NormPriority
  -1,              // 6

  -2,              // 7
  -3,              // 8
  -4,              // 9 NearMaxPriority

  -5,              // 10 MaxPriority

  -5               // 11 CriticalPriority
};

好了,大家應該知道怎麼找出 Java 線程優先級 [1,10] 一一對應各個操作系統中的優先級值。下面給大家統計一下。

Java 線程優先級 Linux Windows Apple Bsd Solaris
1 4 THREAD_PRIORITY_LOWEST(-2) 27 0 0
2 3 THREAD_PRIORITY_LOWEST(-2) 28 3 32
3 2 THREAD_PRIORITY_BELOW_NORMAL(-1) 29 6 64
4 1 THREAD_PRIORITY_BELOW_NORMAL(-1) 30 10 96
5 0 THREAD_PRIORITY_NORMAL(0) 31 15 127
6 -1 THREAD_PRIORITY_NORMAL(0) 32 18 127
7 -2 THREAD_PRIORITY_ABOVE_NORMAL(1) 33 21 127
8 -3 THREAD_PRIORITY_ABOVE_NORMAL(1) 34 25 127
9 -4 THREAD_PRIORITY_HIGHEST(2) 35 28 127
10 -5 THREAD_PRIORITY_HIGHEST(2) 36 31 127

 

我們從這個表格中也可以發現一些問題,即使我們在 Java 代碼中設置了比較高的優先級,其實映射到操作系統的線程裏面,並不一定比設置了低優先級的線程高,很有可能是相同的優先級。看看 Solaris 操作系統 這個極端的例子,優先級 5 到 10 映射的是相同的線程等級。

回頭想想上面的例子爲什麼 3000 個線程,MAX_PRIORITY 優先級的 1000 個線程會優先執行呢?因爲我們的 3 個優先級分別映射到 Windows 操作系統線程的 3 個不同的等級,所以纔會生效。假設將 1、5、10 改成 5、6、7,運行結果那就不大一樣了。

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