多線程2-線程的優先級

線程的優先級

線程的優先級可能影響線程的執行順序,但也不一定會影響
可以設定搶到CPU資源的速度,不一定每次都搶到

public class ThRun implements Runnable{
    public void run(){
        try{
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName()+":"+i);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }
}
//測試
public class ThreadTest{
    public void main(String[] args){
        Thread t1=new Thread(new ThRun(),"A");
        Thread t2=new Thread(new ThRun(),"B");
        Thread t3=new Thread(new ThRun(),"C");
        t1.setPriority(Thread.MIN_PRIORITY);
        t2.setPriority(Thread.NORM_PRIORITY);
        t3.setPriority(Thread.MAX_PRIORITY);
        t1.start();
        t2.start();
        t3.start();
    }
}
發佈了58 篇原創文章 · 獲贊 25 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章