<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國際加速解決方案。 廣告
                [TOC] <br/> > ### 基本概念 * 線程和進程 * 同步和異步 * 并發和并行 * 阻塞和非阻塞 <br/> > ### 線程狀態 ![](https://upload-images.jianshu.io/upload_images/3130736-3d55916e36a4770e?imageMogr2/auto-orient/strip%7CimageView2/2/w/700/format/webp) * 1、**新建狀態**`New`:線程對象被創建后,就進入了新建狀態。例如,`Thread thread = new Thread()` * 2、**就緒狀態**`Runnable`: 也被稱為“可執行狀態”。線程對象被創建后,其它線程調用了該對象的`start()`方法,從而來啟動該線程。例如,`thread.start()`,處于就緒狀態的線程,隨時可能被CPU調度執行。 * 3、**運行狀態**`Running` : 線程獲取CPU權限進行執行。需要注意的是,線程只能從就緒狀態進入到運行狀態。 * 4、**阻塞狀態**`Blocked`: 阻塞狀態是線程因為某種原因放棄CPU使用權,暫時停止運行。直到線程進入就緒狀態,才有機會轉到運行狀態。阻塞的情況分三種: * 等待阻塞 -- 通過調用對象的`wait()`方法、`condition.await`方法。 * 同步阻塞 -- 線程在獲取`synchronized` 同步鎖失敗(因為鎖被其它線程所占用),它會進入同步阻塞狀態。 * 睡眠狀態 -- 通過調用線程的`sleep()`或`join()`或發出了I/O請求時,線程會進入到其他阻塞狀態。當`sleep()`狀態超時、`join()`等待線程終止或者超時、或者I/O處理完畢時,線程重新轉入就緒狀態。 * 5、**死亡狀態**`Dead`:線程執行完畢或者因異常退出了`run()`方法,該線程結束生命周期。 <br/> > ### 使用多線程常見的三種方式 * 集成`Thread`類 ```java public class MyThread extends Thread { @Override public void run() { super.run(); System.out.println("MyThread"); } } public class Run { public static void main(String[] args) { MyThread mythread = new MyThread(); mythread.start(); System.out.println("運行結束"); } } ``` * 實現`Runnable`接口 ```java public class MyRunnable implements Runnable { @Override public void run() { System.out.println("MyRunnable"); } } public class Run { public static void main(String[] args) { Runnable runnable = new MyRunnable(); Thread thread=new Thread(runnable); thread.start(); System.out.println("運行結束!"); } } ``` * 線程池 <br/> >### Thread類中的方法 ```java //使該線程開始執行;Java 虛擬機調用該線程的 run 方法。 public void start() //如果該線程是使用獨立的 Runnable 運行對象構造的,則調用該 Runnable 對象的 run 方法;否則,該方法不執行任何操作并返回。 public void run() //改變線程名稱,使之與參數 name 相同。 public final void setName(String name) //更改線程的優先級。 public final void setPriority(int priority) //將該線程標記為守護線程或用戶線程。 public final void setDaemon(boolean on) //等待該線程終止的時間最長為 millis 毫秒。 public final void join(long millisec) //中斷線程。 public void interrupt() //測試線程是否處于活動狀態。 public final boolean isAlive() //暫停當前正在執行的線程對象,并執行其他線程。 public static void yield() //在指定的毫秒數內讓當前正在執行的線程休眠(暫停執行),此操作受到系統計時器和調度程序精度和準確性的影響。 public static void sleep(long millisec) //返回對當前正在執行的線程對象的引用。 public static Thread currentThread() ```
                  <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>

                              哎呀哎呀视频在线观看