3、线程之间通信

package com.stock.test.base.thread;

import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 *
 * 并发工具包:java.util.concurrent  实例
 * CyclicBarrier  cyclic /'saɪklɪk/ adj. 环的;循环的;周期的
 *                Barrier /'bærɪɚ/ n. 障碍物,屏障;界线 ; vt. 把…关入栅栏
 *
 * CountDownLatch
 *
 */
public class ConcurrentUtilTest {


    /**
     * CyclicBarrier 循环界限
     * 使多个线程在同一时间同时执行
     * @throws IOException
     */
    @Test
    public void cyclicBarrier() throws  InterruptedException {

        CyclicBarrier cyclicBarrier = new CyclicBarrier(4);

        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

        ReentrantReadWriteLock.ReadLock readLock = lock.readLock();
        ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();


        new Thread(new CyclicBarrierThread("线程One",cyclicBarrier,readLock)).start();
        new Thread(new CyclicBarrierThread("线程Two",cyclicBarrier,readLock)).start();
        new Thread(new CyclicBarrierThread("线程Three",cyclicBarrier,readLock)).start();
        new Thread(new CyclicBarrierThread("线程Four",cyclicBarrier,readLock)).start();

        // 提交4个线程之后,被提交的4个线程同事执行

        Thread.sleep(10000);
    }

    public class CyclicBarrierThread implements  Runnable{
        private String name;
        private CyclicBarrier cyclicBarrier;
        private ReentrantReadWriteLock.ReadLock readLock;
        public CyclicBarrierThread(String name, CyclicBarrier cyclicBarrier, ReentrantReadWriteLock.ReadLock readLock){
            this.name = name;
            this.cyclicBarrier = cyclicBarrier;
            this.readLock = readLock;
        }
        @Override
        public void run() {
            try {
                System.out.println(this.name+"--->准备OK");
                cyclicBarrier.await();
                readLock.lock();
                Thread.sleep(2000);
                System.out.println(this.name+"--->GO");
            } catch (InterruptedException | BrokenBarrierException e) {
                e.printStackTrace();
            }finally {
                readLock.unlock();
            }
        }
    }


    /**
     * 一个线程等待多个线程通知继续执行
     * @throws InterruptedException
     */
    @Test
    public void countDownLatch() throws InterruptedException {

        CountDownLatch countDownLatch = new CountDownLatch(2);

        new Thread(new Runnable() {
            @Override
            public void run() {

                System.out.println(Thread.currentThread().getName()+"--->执行");

                try {
                    countDownLatch.await();

                    System.out.println("终于结束了");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        },"线程---1").start();

        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread().getName()+"--->执行");

                    countDownLatch.countDown();

                    System.out.println(Thread.currentThread().getName()+"--->执行通知了");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        },"线程---2").start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(5000);
                    System.out.println(Thread.currentThread().getName()+"--->执行");

                    countDownLatch.countDown();

                    System.out.println(Thread.currentThread().getName()+"--->执行通知了");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        },"线程---3").start();


        Thread.sleep(100000000);

    }


    public class CountDownLatchThread implements  Runnable{


        private String name;

        public CountDownLatchThread(String name){

            this.name = name;

        }

        @Override
        public void run() {

            try {

                System.out.println(this.name+"--->开始执行");

                Thread.sleep(2000);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }





}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值