<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 采用多線程模擬排隊叫號 關于排隊叫號想必大家都了解:比如去銀行,都會有取號機,銀行窗口每辦理完一個業務都會叫下一個排隊的號去處理業務,一個人不會被多個窗口同時叫號,所以這里也涉及到了一個多線程共享數據的問題:我們先用代碼簡單模擬一下流程: ```java /** * @program: ThreadDemo * @description: 模擬柜臺 * @author: hs96.cn@Gmail.com * @create: 2020-08-27 */ public class Bank { public static void main(String[] args) { TicketWindowOneThread ticketWindowOneThread = new TicketWindowOneThread("一號柜臺"); ticketWindowOneThread.start(); } } ``` ```java /** * @program: ThreadDemo * @description: 模擬銀行窗口處理業務 * @author: hs96.cn@Gmail.com * @create: 2020-08-27 */ public class TicketWindowOneThread extends Thread{ private final String name; private static final int MAX = 50; private int index = 1; public TicketWindowOneThread(String name) { this.name = name; } @Override public void run() { // 當當前號碼數小于最大號碼時,進行售票操作 while (index <= MAX) { System.out.println("柜臺:" + name + ",當前的號碼是:" + (index++)); } } } ``` 運行效果如下: ![](https://img.kancloud.cn/01/58/0158128f121ec4c5f2be4b7fb6231ff0_1040x224.gif) 這個輸出毫無疑問,目前是單線程動作,接下來模擬三個窗口來模擬下真實的銀行處理過程: ```java /** * @program: ThreadDemo * @description: 模擬柜臺叫號 * @author: hs96.cn@Gmail.com * @create: 2020-08-27 */ public class Bank { public static void main(String[] args) { TicketWindowOneThread ticketWindowOneThread1 = new TicketWindowOneThread("一號柜臺"); ticketWindowOneThread1.start(); TicketWindowOneThread ticketWindowOneThread2 = new TicketWindowOneThread("二號柜臺"); ticketWindowOneThread2.start(); TicketWindowOneThread ticketWindowOneThread3 = new TicketWindowOneThread("三號柜臺"); ticketWindowOneThread3.start(); } } ``` 運行結果如下: ![](https://img.kancloud.cn/98/96/9896e56d42257e8e8946b69dc7575165_541x282.png) 這時候每個柜臺都按照自己的號來叫,也就是窗口的index沒有在線程之間同步,那么如何解決呢? 其實也很簡單:只要用static修飾一下就好了: ![](https://img.kancloud.cn/a2/9f/a29f9bf13a988cec6730d45927f8795c_744x452.png) 運行效果如下: ![](https://img.kancloud.cn/89/c8/89c8960e6d9c168134e47b795077f1bf_1153x382.gif) 可以看到三個線程已經實現共享`TicketWindowOneThread`的`index`了。但是可以看到輸出的順序顛倒了,這個我們后續再優化,先理解這個線程共享資源的概念即可。
                  <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>

                              哎呀哎呀视频在线观看