java线程:几个实用的线程工具类

 

java线程:几个实用的线程工具类

分类: java_线程   89人阅读  评论(0)  收藏  举报

CyclicBarrier

[java]  view plain copy
  1. import java.util.concurrent.CyclicBarrier;  
  2. import java.util.concurrent.ExecutorService;  
  3. import java.util.concurrent.Executors;  
  4.   
  5. /** 
  6.  * @author amber2012 
  7.  *  
  8.  * CyclicBarrier: 
  9.  * 表示线程彼此等待,等所有的线程都集合后,才开始做任务 
  10.  *  
  11.  */  
  12. public class CyclicBarrierTest {  
  13.   
  14.     public static void main(String[] args) {  
  15.         ExecutorService service = Executors.newCachedThreadPool();  
  16.         final  CyclicBarrier cb = new CyclicBarrier(3);  
  17.         for(int i=0;i<3;i++){  
  18.             Runnable runnable = new Runnable(){  
  19.                     public void run(){  
  20.                     try {  
  21.                         Thread.sleep((long)(Math.random()*10000));    
  22.                         System.out.println("线程" + Thread.currentThread().getName() +   
  23.                                 "即将到达集合地点1,当前已有" + (cb.getNumberWaiting()+1) + "个已经到达," + (cb.getNumberWaiting()==2?"都到齐了,继续走啊":"正在等候"));                         
  24.                         cb.await();  
  25.                           
  26.                         Thread.sleep((long)(Math.random()*10000));    
  27.                         System.out.println("线程" + Thread.currentThread().getName() +   
  28.                                 "即将到达集合地点2,当前已有" + (cb.getNumberWaiting()+1) + "个已经到达," + (cb.getNumberWaiting()==2?"都到齐了,继续走啊":"正在等候"));  
  29.                         cb.await();   
  30.                         Thread.sleep((long)(Math.random()*10000));    
  31.                         System.out.println("线程" + Thread.currentThread().getName() +   
  32.                                 "即将到达集合地点3,当前已有" + (cb.getNumberWaiting() + 1) + "个已经到达," + (cb.getNumberWaiting()==2?"都到齐了,继续走啊":"正在等候"));                       
  33.                         cb.await();                       
  34.                     } catch (Exception e) {  
  35.                         e.printStackTrace();  
  36.                     }                 
  37.                 }  
  38.             };  
  39.             service.execute(runnable);  
  40.         }  
  41.         service.shutdown();  
  42.     }  
  43. }  

CountDownLatch:
[java]  view plain copy
  1. import java.util.concurrent.CountDownLatch;  
  2. import java.util.concurrent.ExecutorService;  
  3. import java.util.concurrent.Executors;  
  4.   
  5. /** 
  6.  * @author amber2012 
  7.  *  
  8.  * CountDownLatch: 
  9.  * 好比倒计时计数器,调用CountDownLatch对象的CountDown方法就将计数器减1,当计数器到达0时,则所有等待线程 
  10.  * 或单个等待线程开始执行。 
  11.  */  
  12. public class CountdownLatchTest {  
  13.   
  14.     public static void main(String[] args) {  
  15.       
  16.         ExecutorService service = Executors.newCachedThreadPool();  
  17.         // 创建两个计数器,cdOrder的初始值为1,cdAnswer初始值为3  
  18.         final CountDownLatch cdOrder = new CountDownLatch(1);  
  19.         final CountDownLatch cdAnswer = new CountDownLatch(3);        
  20.           
  21.         for(int i=0;i<3;i++){  
  22.             Runnable runnable = new Runnable(){  
  23.                     public void run(){  
  24.                     try {  
  25.                         System.out.println("线程" + Thread.currentThread().getName() + "正准备接受命令");                          
  26.                         cdOrder.await(); // 所有的线程都在此等待,并希望被其他线程调用cdOrder.countDown()激活,在这里由主线程激活  
  27.                           
  28.                         System.out.println("线程" + Thread.currentThread().getName() + "已接受命令");                                
  29.                         Thread.sleep((long)(Math.random()*10000));    
  30.                           
  31.                         System.out.println("线程" + Thread.currentThread().getName() + "回应命令处理结果");                         
  32.                         cdAnswer.countDown();// cdAnswer计数器的初始值为3,,三个线程到达后调用cdAnswer.countDown()到计数为0,激活主线程  
  33.                     } catch (Exception e) {  
  34.                         e.printStackTrace();  
  35.                     }                 
  36.                 }  
  37.             };  
  38.             service.execute(runnable);  
  39.         }         
  40.           
  41.         try {  
  42.             Thread.sleep((long)(Math.random()*10000));  
  43.           
  44.             System.out.println("线程" + Thread.currentThread().getName() + "即将发布命令");                       
  45.             cdOrder.countDown();// 主线程将cdOrder计数器减1  
  46.               
  47.             System.out.println("线程" + Thread.currentThread().getName() + "已发送命令,正在等待结果");     
  48.             cdAnswer.await();// 主线程正在等待,希望被其他线程激活  
  49.               
  50.             System.out.println("线程" + Thread.currentThread().getName() + "已收到所有响应结果");    
  51.         } catch (Exception e) {  
  52.             e.printStackTrace();  
  53.         }                 
  54.         service.shutdown();  
  55.     }  
  56. }  

Exchanger:
[java]  view plain copy
  1. import java.util.concurrent.Exchanger;  
  2. import java.util.concurrent.ExecutorService;  
  3. import java.util.concurrent.Executors;  
  4.   
  5. /** 
  6.  * @author amber2012 
  7.  *  
  8.  * Exchanger: 
  9.  * 用于实现两个人之间的数据交换,每个人在完成一定的事务后想与对方交换数据,第一个先拿出数据的人将一直等待第二 
  10.  * 个人拿着数据到来时,才能彼此交换数据。 
  11.  */  
  12. public class ExchangerTest {  
  13.   
  14.     public static void main(String[] args) {  
  15.         ExecutorService service = Executors.newCachedThreadPool();  
  16.         final Exchanger exchanger = new Exchanger();  
  17.           
  18.         service.execute(new Runnable(){  
  19.             public void run() {  
  20.                 try {                 
  21.                     String data1 = "zxx";  
  22.                     System.out.println("线程" + Thread.currentThread().getName() + "正在把数据" + data1 +"换出去");  
  23.                     Thread.sleep(1000L);  
  24.                       
  25.                     String data2 = (String)exchanger.exchange(data1);  
  26.                     System.out.println("线程" + Thread.currentThread().getName() + "换回的数据为" + data2);  
  27.                 }catch(Exception e){  
  28.                 }  
  29.             }     
  30.         });  
  31.           
  32.         service.execute(new Runnable(){  
  33.             public void run() {  
  34.                 try {                 
  35.                     String data1 = "lhm";  
  36.                     System.out.println("线程" + Thread.currentThread().getName() + "正在把数据" + data1 +"换出去");  
  37.                     Thread.sleep(2000L);                      
  38.                       
  39.                     String data2 = (String)exchanger.exchange(data1);  
  40.                     System.out.println("线程" + Thread.currentThread().getName() + "换回的数据为" + data2);  
  41.                 }catch(Exception e){  
  42.                 }                 
  43.             }     
  44.         });       
  45.     }  
  46. }  

猜你喜欢

转载自rainyear.iteye.com/blog/1716723