1、FutureTask 2、CyclicBarrier 3、CountDownLatch 4、Semaphore ...
1、FutureTask
1 package com.ietree.basicskill.mutilthread.OtherClass; 2 3 import java.util.concurrent.*; 4 5 /** 6 * Created by Administrator on 2017/5/17. 7 */ 8 public class UseFuture implements Callable<String> { 9 10 private String para; 11 12 public UseFuture(String para){ 13 this.para = para; 14 } 15 16 /** 17 * 這裡是真實的業務邏輯,其執行可能很慢 18 */ 19 @Override 20 public String call() throws Exception { 21 //模擬執行耗時 22 Thread.sleep(5000); 23 String result = this.para + "處理完成"; 24 return result; 25 } 26 27 //主控制函數 28 public static void main(String[] args) throws Exception { 29 String queryStr = "query"; 30 //構造FutureTask,並且傳入需要真正進行業務邏輯處理的類,該類一定是實現了Callable介面的類 31 FutureTask<String> future = new FutureTask<String>(new UseFuture(queryStr)); 32 33 FutureTask<String> future2 = new FutureTask<String>(new UseFuture(queryStr)); 34 //創建一個固定線程的線程池且線程數為1, 35 ExecutorService executor = Executors.newFixedThreadPool(2); 36 //這裡提交任務future,則開啟線程執行RealData的call()方法執行 37 //submit和execute的區別: 第一點是submit可以傳入實現Callable介面的實例對象, 第二點是submit方法有返回值 38 39 Future f1 = executor.submit(future); //單獨啟動一個線程去執行的 40 Future f2 = executor.submit(future2); 41 System.out.println("請求完畢"); 42 43 try { 44 //這裡可以做額外的數據操作,也就是主程式執行其他業務邏輯 45 System.out.println("處理實際的業務邏輯..."); 46 Thread.sleep(1000); 47 } catch (Exception e) { 48 e.printStackTrace(); 49 } 50 //調用獲取數據方法,如果call()方法沒有執行完成,則依然會進行等待 51 System.out.println("數據:" + future.get()); 52 System.out.println("數據:" + future2.get()); 53 54 executor.shutdown(); 55 } 56 57 58 }
2、CyclicBarrier
1 package com.ietree.basicskill.mutilthread.OtherClass; 2 3 import java.io.IOException; 4 import java.util.Random; 5 import java.util.concurrent.BrokenBarrierException; 6 import java.util.concurrent.CyclicBarrier; 7 import java.util.concurrent.ExecutorService; 8 import java.util.concurrent.Executors; 9 10 /** 11 * Created by Administrator on 2017/5/17. 12 */ 13 public class UseCyclicBarrier { 14 15 static class Runner implements Runnable { 16 private CyclicBarrier barrier; 17 private String name; 18 19 public Runner(CyclicBarrier barrier, String name) { 20 this.barrier = barrier; 21 this.name = name; 22 } 23 @Override 24 public void run() { 25 try { 26 Thread.sleep(1000 * (new Random()).nextInt(5)); 27 System.out.println(name + " 準備OK."); 28 barrier.await(); 29 } catch (InterruptedException e) { 30 e.printStackTrace(); 31 } catch (BrokenBarrierException e) { 32 e.printStackTrace(); 33 } 34 System.out.println(name + " Go!!"); 35 } 36 } 37 38 public static void main(String[] args) throws IOException, InterruptedException { 39 CyclicBarrier barrier = new CyclicBarrier(3); // 3 40 ExecutorService executor = Executors.newFixedThreadPool(3); 41 42 executor.submit(new Thread(new Runner(barrier, "zhangsan"))); 43 executor.submit(new Thread(new Runner(barrier, "lisi"))); 44 executor.submit(new Thread(new Runner(barrier, "wangwu"))); 45 46 executor.shutdown(); 47 } 48 49 }
3、CountDownLatch
1 package com.ietree.basicskill.mutilthread.OtherClass; 2 3 import java.util.concurrent.CountDownLatch; 4 5 /** 6 * Created by Administrator on 2017/5/17. 7 */ 8 public class UseCountDownLatch { 9 10 public static void main(String[] args) { 11 12 final CountDownLatch countDown = new CountDownLatch(2); 13 14 Thread t1 = new Thread(new Runnable() { 15 @Override 16 public void run() { 17 try { 18 System.out.println("進入線程t1" + "等待其他線程處理完成..."); 19 countDown.await(); 20 System.out.println("t1線程繼續執行..."); 21 } catch (InterruptedException e) { 22 e.printStackTrace(); 23 } 24 } 25 },"t1"); 26 27 Thread t2 = new Thread(new Runnable() { 28 @Override 29 public void run() { 30 try { 31 System.out.println("t2線程進行初始化操作..."); 32 Thread.sleep(3000); 33 System.out.println("t2線程初始化完畢,通知t1線程繼續..."); 34 countDown.countDown(); 35 } catch (InterruptedException e) { 36 e.printStackTrace(); 37 } 38 } 39 }); 40 Thread t3 = new Thread(new Runnable() { 41 @Override 42 public void run() { 43 try { 44 System.out.println("t3線程進行初始化操作..."); 45 Thread.sleep(4000); 46 System.out.println("t3線程初始化完畢,通知t1線程繼續..."); 47 countDown.countDown(); 48 } catch (InterruptedException e) { 49 e.printStackTrace(); 50 } 51 } 52 }); 53 54 t1.start(); 55 t2.start(); 56 t3.start(); 57 } 58 }
4、Semaphore
1 package com.ietree.basicskill.mutilthread.OtherClass; 2 3 import java.util.concurrent.ExecutorService; 4 import java.util.concurrent.Executors; 5 import java.util.concurrent.Semaphore; 6 7 /** 8 * Created by Administrator on 2017/5/17. 9 */ 10 public class UseSemaphore { 11 12 public static void main(String[] args) { 13 // 線程池 14 ExecutorService exec = Executors.newCachedThreadPool(); 15 // 只能5個線程同時訪問 16 final Semaphore semp = new Semaphore(5); 17 // 模擬20個客戶端訪問 18 for (int index = 0; index < 20; index++) { 19 final int NO = index; 20 Runnable run = new Runnable() { 21 public void run() { 22 try { 23 // 獲取許可 24 semp.acquire(); 25 System.out.println("Accessing: " + NO); 26 //模擬實際業務邏輯 27 Thread.sleep((long) (Math.random() * 10000)); 28 // 訪問完後,釋放 29 semp.release(); 30 } catch (InterruptedException e) { 31 } 32 } 33 }; 34 exec.execute(run); 35 } 36 37 try { 38 Thread.sleep(10); 39 } catch (InterruptedException e) { 40 e.printStackTrace(); 41 } 42 //System.out.println(semp.getQueueLength()); 43 // 退出線程池 44 exec.shutdown(); 45 } 46 }