標籤:佔用 img 自動 代碼 緩衝 程式 mil exce post
常用的幾種線程池5.1
newCachedThreadPool
建立一個可緩衝線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則建立線程。
這種類型的線程池特點是:
- 背景工作執行緒的建立數量幾乎沒有限制(其實也有限制的,數目為Interger. MAX_VALUE), 這樣可靈活的往線程池中添加線程。
- 如果長時間沒有往線程池中提交任務,即如果背景工作執行緒空閑了指定的時間(預設為1分鐘),則該背景工作執行緒將自動終止。終止後,如果你又提交了新的任務,則線程池重新建立一個背景工作執行緒。
- 在使用CachedThreadPool時,一定要注意控制任務的數量,否則,由於大量線程同時運行,很有會造成系統癱瘓。
範例程式碼如下:
1 package test; 2 import java.util.concurrent.ExecutorService; 3 import java.util.concurrent.Executors; 4 public class ThreadPoolExecutorTest { 5 public static void main(String[] args) { 6 ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); 7 for (int i = 0; i < 10; i++) { 8 final int index = i; 9 try {10 Thread.sleep(index * 1000);11 } catch (InterruptedException e) {12 e.printStackTrace();13 }14 cachedThreadPool.execute(new Runnable() {15 public void run() {16 System.out.println(index);17 }18 });19 }20 }21 }5.2
newFixedThreadPool
建立一個指定背景工作執行緒數量的線程池。每當提交一個任務就建立一個背景工作執行緒,如果背景工作執行緒數量達到線程池初始的最大數,則將提交的任務存入到池隊列中。
FixedThreadPool是一個典型且優秀的線程池,它具有線程池提高程式效率和節省建立線程時所耗的開銷的優點。但是,線上程池空閑時,即線程池中沒有可運行任務時,它不會釋放背景工作執行緒,還會佔用一定的系統資源。
範例程式碼如下:
package test;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class ThreadPoolExecutorTest { public static void main(String[] args) { ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3); for (int i = 0; i < 10; i++) { final int index = i; fixedThreadPool.execute(new Runnable() { public void run() { try { System.out.println(index); Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } }); } }}
因為線程池大小為3,每個任務輸出index後sleep 2秒,所以每兩秒列印3個數字。
定長線程池的大小最好根據系統資源進行設定如Runtime.getRuntime().availableProcessors()。
5.3
newSingleThreadExecutor
建立一個單線程化的Executor,即只建立唯一的工作者線程來執行任務,它只會用唯一的背景工作執行緒來執行任務,保證所有任務按照指定順序(FIFO, LIFO, 優先順序)執行。如果這個線程異常結束,會有另一個取代它,保證順序執行。單背景工作執行緒最大的特點是可保證順序地執行各個任務,並且在任意給定的時間不會有多個線程是活動的。
範例程式碼如下:
package test;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class ThreadPoolExecutorTest { public static void main(String[] args) { ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); for (int i = 0; i < 10; i++) { final int index = i; singleThreadExecutor.execute(new Runnable() { public void run() { try { System.out.println(index); Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } }); } }}
5.4
newScheduleThreadPool
建立一個定長的線程池,而且支援定時的以及周期性的任務執行,支援定時及週期性任務執行。
延遲3秒執行,順延強制範例程式碼如下:
package test;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;public class ThreadPoolExecutorTest { public static void main(String[] args) { ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5); scheduledThreadPool.schedule(new Runnable() { public void run() { System.out.println("delay 3 seconds"); } }, 3, TimeUnit.SECONDS); }}
表示延遲1秒後每3秒執行一次,定期執行範例程式碼如下:
package test;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;public class ThreadPoolExecutorTest { public static void main(String[] args) { ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5); scheduledThreadPool.scheduleAtFixedRate(new Runnable() { public void run() { System.out.println("delay 1 seconds, and excute every 3 seconds"); } }, 1, 3, TimeUnit.SECONDS); }}
Java常用的幾種線程池