Java 建立執行緒池的四種方式
#1、newCachedThreadPool 建立一個可快取執行緒池,如果執行緒池長度超過處理需要,可靈活回收空閒線程,若無可回收,則新線程。
2、newFixedThreadPool 建立一個定長執行緒池,可控制執行緒最大並發數,超出的執行緒會在佇列中等待。
3、newScheduledThreadPool 建立一個定長執行緒池,支援定時及週期性任務執行。
4、newSingleThreadExecutor 建立一個單執行緒化的執行緒池,它只會用唯一的工作執行緒來執行任務,保證所有任務都按照指定順序(FIFO, LIFO, 優先權)執行。
newCachedThreadPool
建立一個可快取執行緒池,如果執行緒池長度超過處理需要,可靈活回收空閒線程,
若無可回收,則新建執行緒。
package cn.qbz.thread; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Test111907 { public static void main(String[] args) { ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) { final int temp = i; executorService.execute(new Runnable() { @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " i=" + temp); } }); } } } public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); }
newFixedThreadPool
建立一個定長執行緒池,可控制執行緒最大並發數,超出的執行緒會在佇列中等待。
package cn.qbz.thread; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Test111907 { public static void main(String[] args) { ExecutorService executorService = Executors.newFixedThreadPool(3); for (int i = 0; i < 10; i++) { final int temp = i; executorService.execute(new Runnable() { @Override public void run() { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " i=" + temp); } }); } } } public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); }
newScheduledThreadPool
建立一個定長執行緒池,支援定時及週期性任務執行。
package cn.qbz.thread; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class Test111907 { public static void main(String[] args) { final long begin = System.currentTimeMillis(); ExecutorService executorService = Executors.newScheduledThreadPool(3); for (int i = 0; i < 10; i++) { final int temp = i; final long time = begin; executorService.schedule(new Runnable() { @Override public void run() { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " i=" + temp + " time=" + (System.currentTimeMillis() - time)); } }, 5, TimeUnit.SECONDS); } } } public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) { return new ScheduledThreadPoolExecutor(corePoolSize); } public ScheduledThreadPoolExecutor(int corePoolSize) { super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS, new DelayedWorkQueue()); }
newSingleThreadExecutor
建立一個單執行緒化的執行緒池,它只會用唯一的工作執行緒來執行任務,
#保證所有任務依照指定順序(FIFO, LIFO, 優先權)執行。
package cn.qbz.thread; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class Test111907 { public static void main(String[] args) { ExecutorService executorService = Executors.newSingleThreadExecutor(); for (int i = 0; i < 10; i++) { final int temp = i; executorService.execute(new Runnable() { @Override public void run() { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " i=" + temp); } }); } } }
推薦教學:《Java教學》
以上是Java 建立線程池的四種方式的詳細內容。更多資訊請關注PHP中文網其他相關文章!