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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ~~~ package thread.a5; public class Test { public static void main(String[] args) { Xc8 xc81=new Xc8(); Thread ee = new Thread(xc81); ee.start(); Xc8 xc82=new Xc8(); Thread ff = new Thread(xc82); ff.start(); } } class Xc8 implements Runnable { public static int chepiao = 100;//static只執行一次,共享100張票,否則會是200張 public void run() { while (true) { if (chepiao > 0) { System.out.println("第"+Thread.currentThread().getName()+"個車站正在賣第"+(101-chepiao)+"張車票"); --chepiao; } else { break; } } } } ~~~ ![](https://box.kancloud.cn/ec8fb29e0c4f9972e0707d76f362a12b_370x94.png) 程序還是有問題,因為程序執行到一半,另一個線程也開啟了 優化代碼如下 ~~~ package thread.a5; public class Test1 { public static void main(String[] args) { Xc9 xc9=new Xc9(); Thread ee = new Thread(xc9); ee.start(); Thread ff = new Thread(xc9); ff.start(); } } class Xc9 implements Runnable { public static int chepiao = 100; String aa = new String("1"); //字符串隨意定義,定義在函數上邊 //synchronized 的作用是,讓它所管轄的代碼部分,要么全部執行完,要么全部不執行 public void run() //synchronized修飾函數不需要字符串,相當于默認是this { while (true) { synchronized (aa) //既可以修飾代碼塊,又可以修飾函數 { if (chepiao > 0) { System.out.println("第"+Thread.currentThread().getName()+"個車站正在賣第"+(101-chepiao)+"張車票"); --chepiao; } else { break; } } } } } ~~~ ![](https://box.kancloud.cn/599aa5da53f3a50c5da2f0f16ce3367f_488x171.png)
                  <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>

                              哎呀哎呀视频在线观看