站長資訊網
        最全最豐富的資訊網站

        Java線程優先級

        Java線程優先級

        Java 線程優先級

        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 源碼中查各個操作系統中線程優先級映射的值。

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

        private native void setPriority0(int newPriority);

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

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

        我們根據 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

        根據第 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); }

        贊(0)
        分享到: 更多 (0)
        網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
        主站蜘蛛池模板: 国产精品www| 丰满人妻熟妇乱又伦精品劲| 精品一区二区久久| 一本色道久久88—综合亚洲精品| 国产精品免费久久| 3D动漫精品一区二区三区| 午夜在线视频91精品| 国内精品久久久久久久涩爱| 国产精品久久久久久福利69堂| 无码日韩精品一区二区免费暖暖| 精品人体无码一区二区三区| 99RE8这里有精品热视频| 国产一区二区三区久久精品| 久久99热只有频精品8| 十八18禁国产精品www| 亚洲AV无码成人网站久久精品大| 亚洲精品国产日韩无码AV永久免费网| 国产在线精品一区免费香蕉| 成人国产精品日本在线观看| 亚洲国产精品久久久久| 久久国产精品一区二区| 国产精品第13页| 精品福利资源在线| 国产精品亚洲精品观看不卡| 国产成人精品日本亚洲网址| 国产精品福利网站导航| 国产91精品在线观看| 99精品视频在线观看re| 高清在线亚洲精品国产二区| 国产成人精品久久二区二区| 97久久久精品综合88久久| 91精品国产综合久久精品| 99国产精品一区二区| 国产欧美久久久精品| 秋霞久久国产精品电影院| 99久久99久久精品国产片| 777欧美午夜精品影院| 国产美女精品视频| 51视频国产精品一区二区| 亚洲AV永久无码精品网站在线观看| 亚洲日韩精品无码专区网站|