java 線程協作 yield(),java線程協作yield

來源:互聯網
上載者:User

java 線程協作 yield(),java線程協作yield

 

yield():方法的定義

調用yield方法會讓當前線程交出CPU許可權,讓CPU去執行其他的線程。 但是yield不能控制具體的交出CPU的時間,另外,yield方法只能讓擁有相同優先順序的線程有擷取CPU執行時間的機會 (Yield告訴當前正在執行的線程把運行機會交給線程池中擁有相同優先順序的線程, cpu會從眾多的可執行態裡選擇.),優先順序高的線程業不一定會獲得執行權,他們只是機率上大些。該方法不會釋放鎖。
注意:調用yield方法並不會讓線程進入阻塞狀態,而是讓線程重回就緒狀態,它只需要等待重新擷取CPU執行時間(也就是說,當前也就是剛剛的那個線程還是有可能會被再次執行到的,
並不是說一定會執行其他線程而該線程在下一次中不會執行到了)。

樣本:交出當前cpu的執行權,與其他線程一起重新擷取cpu的調度
public class ThreadYieldTest {    static Object object=new Object();    public static void main(String[] args) {        Thread thread_a=new Thread(new Thread_a());        Thread thread_b=new Thread(new Thread_b());        thread_a.setPriority(Thread.MIN_PRIORITY); //Min Priority        thread_b.setPriority(Thread.MAX_PRIORITY); //Max Priority        thread_a.start();        thread_b.start();    }}class Thread_a implements Runnable{    @Override    public void run() {        try {                   for(int i=0;i<5;i++){                    //當i為3時,該線程就會把CPU時間讓掉,讓其他或者自己的線程執行(也就是誰先搶到誰執行)                      if(i==3){                        Thread.yield();                    }                    System.out.println(Thread.currentThread().getName()+"執行==》"+i);                }        } catch (Exception e) {            e.printStackTrace();        }    }}class Thread_b implements Runnable{    @Override    public void run() {        try {                 for(int i=0;i<5;i++){                    if(i==3){                        Thread.yield();                    }                    System.out.println(Thread.currentThread().getName()+"執行==》"+i);                }        } catch (Exception e) {            e.printStackTrace();        }    }} 

運行結果:

 第一種情況:Thread-0 當執行到3時會CPU時間讓掉,這時Thread-1搶到CPU時間並執行。

 第二種情況:Thread-0 當執行到3時會CPU時間讓掉,這時Thread-0搶到CPU時間並執行。

樣本:它跟sleep方法類似,同樣不會釋放鎖。

public class ThreadYieldTest {    static Object object=new Object();    public static void main(String[] args) {        Thread thread_a=new Thread(new Thread_a());        Thread thread_b=new Thread(new Thread_b());        thread_a.setPriority(Thread.MIN_PRIORITY); //Min Priority        thread_b.setPriority(Thread.MAX_PRIORITY); //Max Priority        thread_a.start();        thread_b.start();    }}class Thread_a implements Runnable{    @Override    public void run() {        try {               synchronized (ThreadYieldTest.object) {                System.out.println("進入線程"+Thread.currentThread().getName());                for(int i=0;i<5;i++){                    //當i為3時,該線程就會把CPU時間讓掉,讓其他或者自己的線程執行(也就是誰先搶到誰執行)                      if(i==3){                        Thread.yield();                    }                    System.out.println(Thread.currentThread().getName()+"執行==》"+i);                }            }        } catch (Exception e) {            e.printStackTrace();        }    }}class Thread_b implements Runnable{    @Override    public void run() {        try {              synchronized (ThreadYieldTest.object) {                System.out.println("進入線程"+Thread.currentThread().getName());                for(int i=0;i<5;i++){                    if(i==3){                        Thread.yield();                    }                    System.out.println(Thread.currentThread().getName()+"執行==》"+i);                }            }        } catch (Exception e) {            e.printStackTrace();        }    }}

執行結果:無論如何重複執行,一個線程只能等另一個線程執行完後才能執行。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.