<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] # 簡介 多個線程在處理同一個資源,但是處理的動作(線程的任務)卻不相同。通過一定的手段使各個線程能有效的利用資源。而這種手段即—— 等待喚醒機制。 等待喚醒機制所涉及到的方法: ~~~ * wait() :等待,將正在執行的線程釋放其執行資格 和 執行權,并存儲到線程池中 * notify():喚醒,喚醒線程池中被wait()的線程,一次喚醒一個,而且是任意的 * notifyAll(): 喚醒全部:可以將線程池中的所有wait() 線程都喚醒。 ~~~ 其實,**所謂喚醒的意思就是讓線程池中的線程具備執行資格**。必須注意的是,這些方法都是在 同步中才有效。同時這些方法在使用時必須標明所屬鎖,這樣才可以明確出這些方法操作的到底是哪個鎖上的線程。 仔細查看JavaAPI之后,發現這些方法 并不定義在 Thread中,也沒定義在Runnable接口中,卻被定義在了Object類中,為什么這些操作線程的方法定義在Object類中? 因為這些方法在使用時,必須要標明所屬的鎖,而鎖又可以是任意對象。能被任意對象調用的方法一定定義在Object類中。 ~~~ void notify() 喚醒在此對象監視器上等待的單個線程 void notifyAll() 喚醒在此對象監視器上等待的所有線程 void wait() 在其他線程調用此對象的notify()方法或notifyAll()方法前,導致當前線程等待 ~~~ # 例子 ![](https://box.kancloud.cn/b2568a630f6681c57289bb39dc86f78a_931x329.png) 如上圖說示,輸入線程向Resource中輸入name ,sex , 輸出線程從資源中輸出,先要完成的任務是: * 1.當input發現Resource中沒有數據時,開始輸入,輸入完成后,叫output來輸出。如果發現有數據,就wait(); * 2.當output發現Resource中沒有數據時,就wait() ;當發現有數據時,就輸出,然后,叫醒input來輸入數據。 那個對象等待的就用那個對象喚醒 下面代碼,模擬等待喚醒機制的實現 * 模擬資源類 ~~~ public class Resource { private String name; private String sex; private boolean flag = false; public synchronized void set(String name, String sex) { //這邊不是if else, wait之后,喚醒要set,不然少一次set if (flag) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // 設置成員變量 this.name = name; this.sex = sex; // 設置之后,Resource中有值,將標記該為 true , flag = true; // 喚醒output this.notify(); } public synchronized void out() { if (!flag) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // 輸出線程將數據輸出 System.out.println("姓名: " + name + ",性別: " + sex); // 改變標記,以便輸入線程輸入數據 flag = false; // 喚醒input,進行數據輸入 this.notify(); } } ~~~ * 輸入線程任務類 ~~~ public class Input implements Runnable { private Resource r; public Input(Resource r) { this.r = r; } @Override public void run() { int count = 0; while (true) { if (count == 0) { r.set("小明", "男生"); } else { r.set("小花", "女生"); } // 在兩個數據之間進行切換 count = (count + 1) % 2; } } } ~~~ * 輸出線程任務類 ~~~ public class Output implements Runnable { private Resource r; public Output(Resource r) { this.r = r; } @Override public void run() { while (true) { r.out(); } } } ~~~ * 測試類 ~~~ public class ResourceDemo { public static void main(String[] args) { // 資源對象 Resource r = new Resource(); // 任務對象 Input in = new Input(r); Output out = new Output(r); // 線程對象 Thread t1 = new Thread(in); Thread t2 = new Thread(out); // 開啟線程 t1.start(); t2.start(); } } ~~~ # wait帶參數 進入TimeWaiting(計時等待)有兩種方式 1. 使用sleep(Long m)方法,在毫秒值結束后,線程睡眠后進入到Runnable/Blocked狀態 2. 使用wait(Long m)方法,wait方法如果在毫秒值結束后,還沒有被notify喚醒,就自動醒來,線程睡醒進入Runnable/Blocked狀態 # sleep和wait區別 1. 方法聲明 ~~~ Thread.sleep() ~~~ ~~~ Object.sleep() ~~~ 2. 共同點 當前線程進入阻塞狀態 3. 使用范圍 sleep使用沒有情景要求, wait必須在同步代碼塊或同步方法中 4. 都在同步中用 wait需要喚醒,會釋放鎖 sleep不需要喚醒,不會釋放鎖
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看