<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之旅 廣告
                #### 11.3.2 線程池的分類 在11.3.1節中對ThreadPoolExecutor的配置細節進行了詳細的介紹,本節將接著介紹**Android中最常見的四類具有不同功能特性的線程池,它們都直接或間接地通過配置ThreadPoolExecutor來實現自己的功能特性**,這四類線程池分別是**FixedThreadPool、CachedThreadPool、ScheduledThreadPool以及SingleThreadExecutor**。 在JDK 的java.util.concurrent.Executors 中提供了生成多種線程池的靜態方法,然后調用他們的execute 方法即可。 ``` ExecutorService newCachedThreadPool = Executors.newCachedThreadPool(); ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(4); ScheduledExecutorService newScheduledThreadPool =Executors.newScheduledThreadPool(4); ExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor(); ``` * [ ] 1、**FixedThreadPooI** 通過[Executors](https://www.androidos.net.cn/android/6.0.1_r16/xref/libcore/luni/src/main/java/java/util/concurrent/Executors.java)的`newFixedThreadPool`方法來創建。它是一種**線程數量固定的線程池**, * 當**線程處于空閑狀態時,它們并不會被回收,除非線程池被關閉了**。 * 當**所有的線程都處于活動狀態時,新任務都會處于等待狀態,直到有線程空閑出來**。 由于**FixedThreadPool只有核心線程并且這些核心線程不會被回收**,這意味著**它能夠更加快速地響應外界的請求**。newFixedThreadPool方法的實現如下,可以發現**FixedThreadPool中只有核心線程并且這些核心線程沒有超時機制,另外任務隊列也是沒有大小限制**的。 ``` public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); } ``` * [ ] 2、**CachedThreadPooI** 通過Executors的`newCachedThreadPool`方法來創建。 它是一種**線程數量不定的線程池,它只有非核心線程,并且其最大線程數為`Integer.MAX_VALUE`**。由于`Integer.MAX_VALUE`是一個很大的數,實際上就**相當于最大線程數可以任意大**。 **當線程池中的線程都處于活動狀態時,線程池會創建新的線程來處理新任務,否則就會利用空閑的線程來處理新任務。線程池中的空閑線程都有超時機制,這個超時時長為60秒,超過60秒閑置線程就會被回收**。 和FixedThreadPool不同的是,**CachedThreadPool的任務隊列其實相當于一個空集合,這將導致任何任務都會立即被執行,因為在這種場景下SynchronousQueue是無法插入任務的**。 *SynchronousQueue是一個非常特殊的隊列,在很多情況下可以把它簡單理解為一個無法存儲元素的隊列,由于它在實際中較少使用,這里就不深入探討它了*。從**CachedThreadPoo**l的特性來看,這類線程池**比較適合執行大量的耗時較少的任務。當整個線程池都處于閑置狀態時,線程池中的線程都會超時而被停止,這個時候CachedThreadPool之中實際上是沒有任何線程的,它幾乎是不占用任何系統資源的**。newCachedThreadPool方法的實現如下所示。 ``` public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); } ``` * 3、**ScheduIedThreadPooI** 通過Executors的`newScheduledThreadPool`方法來創建。它的**核心線程數量是固定的,而非核心線程數是沒有限制的,并且當非核心線程閑置時會被立即回收**。ScheduledThreadPool這類線程池**主要用于執行定時任務和具有固定周期的重復任務**,newScheduledThreadPool方法的實現如下所示。 ``` public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) { return new ScheduledThreadPoolExecutor(corePoolSize); } public ScheduledThreadPoolExecutor(int corePoolSize) { super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,new DelayedWorkQueue()); } ``` * 4、**SingIeThreadExecutor** 通過Executors的`newSingleThreadExecutor`方法來創建。這類**線程池內部只有一個核心線程,它確保所有的任務都在同一個線程中按順序執行**。SingleThreadExecutor的**意義在于統一所有的外界任務到一個線程中,這使得在這些任務之間不需要處理線程同步的問題**。newSingleThreadExecutor方法的實現如下所示。 ``` public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())); } ``` 上面對Android中常見的4種線程池進行了詳細的介紹,除了上面系統提供的4類線程池以外,也可以根據實際需要靈活地配置線程池。下面的代碼演示了系統預置的4種線程池的典型使用方法。 ``` Runnable command = new Runnable() { @Override public void run() { SystemClock.sleep(2000); } }; ExecutorService fixedThreadPool = Executors.newFixedThreadPool(4); fixedThreadPool.execute(command); ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); cachedThreadPool.execute(command); ScheduledExecutorService scheduledThreadPool=Executors.newScheduledThreadPool(4); // 2000ms后執行command scheduledThreadPool.schedule(command, 2000, TimeUnit.MILLISECONDS); // 延遲10ms后,每隔1000ms執行一次command scheduledThreadPool.scheduleAtFixedRate(command, 10, 1000, TimeUnit.MILLISECONDS); ExecutorService singleThreadExecutor =Executors.newSingleThreadExecutor(); singleThreadExecutor.execute(command); ```
                  <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>

                              哎呀哎呀视频在线观看