java併發編程–Executor框架(一)
摘要:
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) //後兩個參數爲可選參數
public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, //使用一個基於FIFO排序的阻塞隊列,在所有corePoolSize線程都忙時新任務將在隊列中等待 new LinkedBlockingQueue<Runnable>()); }
public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService //corePoolSize和maximumPoolSize都等於,表示固定線程池大小爲1 (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())); }
1 public class HeartBeat { 2 public static void main(String[] args) { 3 ScheduledExecutorService executor = Executors.newScheduledThreadPool(5); 4 Runnable task = new Runnable() { 5 public void run() { 6 System.out.println("HeartBeat........................."); 7 } 8 }; 9 executor.scheduleAtFixedRate(task,5,3, TimeUnit.SECONDS); //5秒後第一次執行,之後每隔3秒執行一次 10 } 11 }
HeartBeat....................... //5秒後第一次輸出 ........ //每隔3秒輸出一個
public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, //使用同步隊列,將任務直接提交給線程 new SynchronousQueue<Runnable>()); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class ThreadPoolTest { public static void main(String[] args) throws InterruptedException { ExecutorService threadPool = Executors.newCachedThreadPool(); //線程池裏面的線程數會動態變化,並可在線程線被移除前重用 for ( int i = 1 ; i <= 3 ; i ++) { final int task = i; //10個任務 //TimeUnit.SECONDS.sleep(1); threadPool.execute( new Runnable() { //接受一個Runnable實例 public void run() { System.out.println( "線程名字: " + Thread.currentThread().getName() + " 任務名爲: " +task); } }); } } } |
線程名字: pool-1-thread-1 任務名爲: 1 線程名字: pool-1-thread-2 任務名爲: 2 線程名字: pool-1-thread-3 任務名爲: 3
線程名字: pool-1-thread-1 任務名爲: 1 線程名字: pool-1-thread-1 任務名爲: 2 線程名字: pool-1-thread-1 任務名爲: 3
1 2 3 4 5 6 7 8 9 10 11 | public class CallableAndFuture { public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executor = Executors.newSingleThreadExecutor(); Future<String> future = executor.submit( new Callable<String>() { //接受一上callable實例 public String call() throws Exception { return "MOBIN" ; } }); System.out.println( "任務的執行結果:" +future.get()); } } |
任務的執行結果:MOBIN
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class CompletionServiceTest { public static void main(String[] args) throws InterruptedException, ExecutionException { ExecutorService executor = Executors.newFixedThreadPool( 10 ); //創建含10.條線程的線程池 CompletionService completionService = new ExecutorCompletionService(executor); for ( int i = 1 ; i <= 10 ; i ++) { final int result = i; completionService.submit( new Callable() { public Object call() throws Exception { Thread.sleep( new Random().nextInt( 5000 )); //讓當前線程隨機休眠一段時間 return result; } }); } System.out.println(completionService.take().get()); //獲取執行結果 } } |
3