并发包 Semaphore,CountDownLatch,CyclicBarrier,Exchanger 实例:

版权声明:取之网络,分享之网络. https://blog.csdn.net/qq_37751454/article/details/90240754

  Semaphore,CountDownLatch,CyclicBarrier,Exchanger

import java.util.concurrent.*;

/**
 * Created by HuaWeiBo on 2019/5/13.
 */
public class Test {

    /**
     * 限制数量:多余的进行等待
     */
    public static void semaphore() {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        Semaphore semaphore = new Semaphore(3);
        for (int i = 0; i < 10; i++) {
            int finalI = i;
            executorService.execute(new Thread(()->{
                try {
                    // 只能运行三个线程
                    semaphore.acquire();
                    System.out.println("i:" + finalI);
                    Thread.sleep(1000);
                    semaphore.release();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }));
        }
        executorService.shutdown();
    }

    /**
     * 信号枪:统一执行
     * @param count
     */
    public static void countDownLatch(int count){
        CountDownLatch cdl = new CountDownLatch(count);
        for (int i = 0; i < count; i++) {
            int finalI = i;
            new Thread(()->{
                try {
                    System.out.println("---------进入等待:" + finalI);
                    cdl.await();
                    System.out.println("一起执行:" + finalI);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
            try {
                // 避免打印结果混乱
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            cdl.countDown();
        }
    }

    /**
     * 信号枪:统一执行
     * @param count
     */
    public static void cyclicBarrier(int count){
        CyclicBarrier cyclicBarrier = new CyclicBarrier(count);
        for (int i = 0; i < count; i++) {
            int finalI = i;
            new Thread(()->{
                try {
                    System.out.println("---------进入等待:" + finalI);
                    cyclicBarrier.await();
                    System.out.println("开始奔跑:" + finalI);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

    /**
     * 交换两个线程的数据
     */
    public static void exchanger() {
        Exchanger<String> exchanger = new Exchanger<>();
        Thread a = new ExchangerThread(exchanger, "A", "a");
        Thread b = new ExchangerThread(exchanger, "B", "b");
        a.start();
        b.start();
    }

    public static void main(String[] args) throws Exception{
        //semaphore();
        //countDownLatch(100);
        //cyclicBarrier(100);
        exchanger();
    }

    public static class ExchangerThread extends Thread{
        Exchanger<String> exchanger;
        String name;
        String value;

        public ExchangerThread(Exchanger<String> exchanger, String name, String value) {
            this.exchanger = exchanger;
            this.value = value;
            this.name = name;
        }

        @Override
        public void run() {
            try {
                String exchange = exchanger.exchange(value);
                System.out.println("name:" + name + ",value:" + exchange);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

猜你喜欢

转载自blog.csdn.net/qq_37751454/article/details/90240754