java-concurrent包(JUC)

https://blog.csdn.net/u013851082/article/details/68488640

https://blog.csdn.net/wbwjx/article/details/57856045

 

Demo:

package com.buaahy.juc;

import java.util.concurrent.*;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author hy
 * @date 2018-08-06 16:51
 */
public class JUCDemo {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<String>(100);
        new Thread(() -> blockingQueue.add("阻塞队列中的一个元素")).start();
        new Thread(() -> {
            try {
                System.out.println(blockingQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        //闭锁测试
        CountDownLatch countDownLatch = new CountDownLatch(3);
        new Thread(() -> {
            try {
                System.out.println("线程开始等待,等待倒计时结束");
                countDownLatch.await();
                System.out.println("线程等待结束,开始运行");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(() -> {
            System.out.println("倒计时开始");
            while (countDownLatch.getCount() > 0) {
                System.out.println(countDownLatch.getCount());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                countDownLatch.countDown();
            }
            System.out.println("倒计时结束");
        }).start();


        //读写锁测试  两个线程加读锁  一个线程加写锁  发现读锁上还可以加读锁  而写锁必须等到加在上面的所有锁释放后才能获得锁
        ReadWriteLock lock = new ReentrantReadWriteLock();
        new Thread(() -> {
            System.out.println("开始加读锁1");
            lock.readLock().lock();
            System.out.println("读锁1加成功");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lock.readLock().unlock();
            System.out.println("读锁1释放");
        }).start();

        new Thread(() -> {
            System.out.println("开始加读锁2");
            lock.readLock().lock();
            System.out.println("读锁2加成功");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lock.readLock().unlock();
            System.out.println("读锁2释放");
        }).start();

        new Thread(() -> {
            System.out.println("开始加写锁");
            lock.writeLock().lock();
            System.out.println("写锁加成功");
            lock.writeLock().unlock();
            System.out.println("写锁释放成功");
        }).start();

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //信号量测试 只有一个许可(一个线程获得后,其他线程获取要等待) 非公平的
        Semaphore semaphore = new Semaphore(1, false);
        new Thread(() -> {
            try {
                System.out.println("线程1尝试获得许可");
                semaphore.acquire();
                System.out.println("线程1获得许可成功");
                Thread.sleep(2000);
                System.out.println("线程1开始释放许可");
                semaphore.release();
                System.out.println("线程1释放许可成功");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            try {
                System.out.println("线程2尝试获得信号量");
                semaphore.acquire();
                System.out.println("线程2获得信号量成功");
                semaphore.release();//使用完后释放
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        //栅栏测试
        CyclicBarrier cyclicBarrier = new CyclicBarrier(2, new Runnable() {
            @Override
            public void run() {
                System.out.println("栅栏被推翻了,开始执行。");
            }
        });

        cyclicBarrier.isBroken();
        new Thread(() -> {
            try {
                System.out.println("栅栏线程1开始等待");
                cyclicBarrier.await();
                System.out.println("栅栏线程1等待结束,继续执行");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(() -> {
            try {
                System.out.println("当前等待线程数:" + cyclicBarrier.getNumberWaiting());
                System.out.println("栅栏线程2开始等待");
                cyclicBarrier.await();
                System.out.println("栅栏线程2等待结束,继续执行");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }).start();

        System.out.println("Finished!");


    }
}

 

发表评论

邮箱地址不会被公开。 必填项已用*标注