JAVA 多線程編程 --線程的調度策略

JAVA中一個線程有自己的優先級(priority)
getPriority:返回當前線程的優先級
setPriority:設置當前線程的優先級
 
既然JAVA支持多線程,那麼多個線程同時執行的時候必然涉及到調度的問題。
通過研究官方文檔和書籍,我發現:JAVA的調度算法非常簡單:
 
1.選擇當前可運行線程中優先級最高的線程運行
2.擁有同樣優先級的線程:採用round-robin的方式。
 
JAVA是preemptive的,但是不同平臺的實現並不一定能保證這一點。
下面是一段引用:
For the runtime on a Solaris Operating Environment platform,Java technology does not preempt threads of the same priority.However,the runtime on Microsoft Windows platforms uses time-slicing,so it preempts threads of the same priority and even threads of higher priority.Preemption is not guaranteed;however,most JVM implementations result in behavior that appears to be strictly preemptive.Across JVM implementations,there is no absolute guarantee of preemption or time-slicing.The only guarantees lie in the coder’s use of wait and sleep. The model of a preemptive scheduler is that many threads might be runnable,but only one thread is actually running.This thread continues to run until it ceases to be runnable or another thread of higher priority becomes runnable.In the latter case,the lower priority thread is preempted by the thread of higher priority,which gets a chance to run instead.
 
看了這段引用後,發現討論JAVA線程的調度似乎沒有什麼意義了,呵呵。
下面我是我的一個運行結果。我是win7,雙核處理器。從結果看出來,基本還是滿足前面兩點的。
package multithread;


public class PrioritedThread extends Thread{
   
    public PrioritedThread(int i){
        if(i >0 && i < 11){
            this.setPriority(i);
        }
    }
   
    public void run(){
        int i = 0;
        while(true){
            if(i == 100000000){
                System.out.print(this.getPriority() + "\t");
                i = 0;
            }
            i++;
        }
    }


}


 
package multithread;


public class TestPriority {


   
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        for(int i = 1; i < 11; i++){
            Thread temp = new PrioritedThread(i);
            temp.start();
        }


    }


}


運行結果:
10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    8    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    7    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    4    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    6    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    3    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    2    9    10    10    9    10    10    9    10    10    9    10    10    10    9    10    10    10    9    10    10    9    10    10    8    10    10    8    10    10    8    10    10    8   

發佈了160 篇原創文章 · 獲贊 27 · 訪問量 31萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章