CyclicBarrier
- import java.util.concurrent.CyclicBarrier;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- /**
- * @author amber2012
- *
- * CyclicBarrier:
- * 表示线程彼此等待,等所有的线程都集合后,才开始做任务
- *
- */
- public class CyclicBarrierTest {
- public static void main(String[] args) {
- ExecutorService service = Executors.newCachedThreadPool();
- final CyclicBarrier cb = new CyclicBarrier(3);
- for(int i=0;i<3;i++){
- Runnable runnable = new Runnable(){
- public void run(){
- try {
- Thread.sleep((long)(Math.random()*10000));
- System.out.println("线程" + Thread.currentThread().getName() +
- "即将到达集合地点1,当前已有" + (cb.getNumberWaiting()+1) + "个已经到达," + (cb.getNumberWaiting()==2?"都到齐了,继续走啊":"正在等候"));
- cb.await();
- Thread.sleep((long)(Math.random()*10000));
- System.out.println("线程" + Thread.currentThread().getName() +
- "即将到达集合地点2,当前已有" + (cb.getNumberWaiting()+1) + "个已经到达," + (cb.getNumberWaiting()==2?"都到齐了,继续走啊":"正在等候"));
- cb.await();
- Thread.sleep((long)(Math.random()*10000));
- System.out.println("线程" + Thread.currentThread().getName() +
- "即将到达集合地点3,当前已有" + (cb.getNumberWaiting() + 1) + "个已经到达," + (cb.getNumberWaiting()==2?"都到齐了,继续走啊":"正在等候"));
- cb.await();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- };
- service.execute(runnable);
- }
- service.shutdown();
- }
- }
CountDownLatch:
- import java.util.concurrent.CountDownLatch;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- /**
- * @author amber2012
- *
- * CountDownLatch:
- * 好比倒计时计数器,调用CountDownLatch对象的CountDown方法就将计数器减1,当计数器到达0时,则所有等待线程
- * 或单个等待线程开始执行。
- */
- public class CountdownLatchTest {
- public static void main(String[] args) {
- ExecutorService service = Executors.newCachedThreadPool();
- // 创建两个计数器,cdOrder的初始值为1,cdAnswer初始值为3
- final CountDownLatch cdOrder = new CountDownLatch(1);
- final CountDownLatch cdAnswer = new CountDownLatch(3);
- for(int i=0;i<3;i++){
- Runnable runnable = new Runnable(){
- public void run(){
- try {
- System.out.println("线程" + Thread.currentThread().getName() + "正准备接受命令");
- cdOrder.await(); // 所有的线程都在此等待,并希望被其他线程调用cdOrder.countDown()激活,在这里由主线程激活
- System.out.println("线程" + Thread.currentThread().getName() + "已接受命令");
- Thread.sleep((long)(Math.random()*10000));
- System.out.println("线程" + Thread.currentThread().getName() + "回应命令处理结果");
- cdAnswer.countDown();// cdAnswer计数器的初始值为3,,三个线程到达后调用cdAnswer.countDown()到计数为0,激活主线程
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- };
- service.execute(runnable);
- }
- try {
- Thread.sleep((long)(Math.random()*10000));
- System.out.println("线程" + Thread.currentThread().getName() + "即将发布命令");
- cdOrder.countDown();// 主线程将cdOrder计数器减1
- System.out.println("线程" + Thread.currentThread().getName() + "已发送命令,正在等待结果");
- cdAnswer.await();// 主线程正在等待,希望被其他线程激活
- System.out.println("线程" + Thread.currentThread().getName() + "已收到所有响应结果");
- } catch (Exception e) {
- e.printStackTrace();
- }
- service.shutdown();
- }
- }
Exchanger:
- import java.util.concurrent.Exchanger;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- /**
- * @author amber2012
- *
- * Exchanger:
- * 用于实现两个人之间的数据交换,每个人在完成一定的事务后想与对方交换数据,第一个先拿出数据的人将一直等待第二
- * 个人拿着数据到来时,才能彼此交换数据。
- */
- public class ExchangerTest {
- public static void main(String[] args) {
- ExecutorService service = Executors.newCachedThreadPool();
- final Exchanger exchanger = new Exchanger();
- service.execute(new Runnable(){
- public void run() {
- try {
- String data1 = "zxx";
- System.out.println("线程" + Thread.currentThread().getName() + "正在把数据" + data1 +"换出去");
- Thread.sleep(1000L);
- String data2 = (String)exchanger.exchange(data1);
- System.out.println("线程" + Thread.currentThread().getName() + "换回的数据为" + data2);
- }catch(Exception e){
- }
- }
- });
- service.execute(new Runnable(){
- public void run() {
- try {
- String data1 = "lhm";
- System.out.println("线程" + Thread.currentThread().getName() + "正在把数据" + data1 +"换出去");
- Thread.sleep(2000L);
- String data2 = (String)exchanger.exchange(data1);
- System.out.println("线程" + Thread.currentThread().getName() + "换回的数据为" + data2);
- }catch(Exception e){
- }
- }
- });
- }
- }