JAVA多线程(二)线程锁

1、ReentrantLock

public class LockTest {
    static Map<String, Integer> map = new ConcurrentHashMap<String, Integer>();

    Lock lock = new ReentrantLock();

    private void testReentrantLock() {
        try {
            lock.lock();

        } catch (Throwable e) {

        } finally {
            lock.unlock();
        }
    }

2、CountDownLatch

    private static void testCountDownLatch() throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(2);
        Thread thread = new Thread(() -> {
            System.out.println("1111");
            countDownLatch.countDown();
        }, "12345678");
        Thread thread2 = new Thread(() -> {
            System.out.println("22222");
            countDownLatch.countDown();
        }, "2345678");
        thread2.start();


        thread.start();
        countDownLatch.await();
        System.out.println("123456789");
    }

3、CyclicBarrier

    private static void testCyclicBarrier() {

        CyclicBarrier cyclicBarrier = new CyclicBarrier(5, () -> {
            Set<String> set = map.keySet();
            System.out.println(set.size());

            //Set<Map.Entry<String,Integer>> entrySet=map.entrySet();
//            for(Map.Entry<String,Integer> a:entrySet){
//                System.out.println(a.getKey());
//                System.out.println(a.getValue());
//            }
        });
        for (int i = 0; i < 100; i++) {

            new Thread(() -> {
                try {
                    map.put(Thread.currentThread().getName(), 12345);
                    cyclicBarrier.await();

                    map.put(Thread.currentThread().getName(), 12345);
                    //barrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();

        }


        // System.out.println("123456789");
    }

4、Phaser

    private static void testPhaser() {
        Phaser phaser = new Phaser(2);
        Thread thread = new Thread(() -> {
            System.out.println("1111");
            phaser.arrive();
        }, "12345678");
        Thread thread2 = new Thread(() -> {
            System.out.println("22222");
            phaser.arrive();
        }, "2345678");
        thread2.start();


        thread.start();
        phaser.awaitAdvance(phaser.getPhase());
        System.out.println("123456789");

    }

    private static void testPhaser2() {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        Phaser phaser = new Phaser(5);
        for (int i = 0; i < 100; i++) {

            new Thread(() -> {
                if (map.size() == 5) {
                    try {
                        countDownLatch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                map.put(Thread.currentThread().getName(), 1234);
                System.out.println("111111111");
                phaser.arriveAndAwaitAdvance();
                System.out.println("=========");
                if (map.size() == 5) {
                    countDownLatch.countDown();
                }


            }).start();

        }


    }

5、ReadWriteLock

    private static void testReadWriteLock() {
        ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

        Lock readLock = readWriteLock.readLock();
        Lock writeLock = readWriteLock.writeLock();
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {

                readLock.lock();

                System.out.println("read start");

                System.out.println("read end");
                readLock.unlock();

            }, "read").start();
        }
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {

                writeLock.lock();
                System.out.println("write start");

                System.out.println("write end");

                writeLock.unlock();

            }, "write").start();
        }
    }

6、Semaphore

    private static void testSemaphore() {
        Semaphore semaphore = new Semaphore(2, true);
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                try {

                    semaphore.acquire();
                    System.out.println("1111111111");
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println("==========");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    semaphore.release();
                }
            }, "write").start();
        }
    }

7、Exchanger

    private static void testExchanger() {
        Exchanger<String> exchanger = new Exchanger<>();
        new Thread(() -> {

            String a = "a";
            try {
                System.out.println(Thread.currentThread().getName() + " " + a);
                a = exchanger.exchange(a);
                System.out.println(Thread.currentThread().getName() + " " + a);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }, "read").start();


        new Thread(() -> {
            String a = "b";
            try {
                System.out.println(Thread.currentThread().getName() + " " + a);
                a = exchanger.exchange(a);
                System.out.println(Thread.currentThread().getName() + " " + a);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }, "write").start();
    }

8、LockSupport

    private static void testLockSupport() {
        Thread a =new Thread(() -> {
            System.out.println("1111");
            LockSupport.park();
            System.out.println("2222");
        }, "123");
        a.start();

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Thread b =new Thread(() -> {
            System.out.println("3333");
            LockSupport.unpark(a);
            System.out.println("4444");
        }, "1234");
        b.start();
    }

测试方法

    public static void main(String[] args) throws InterruptedException {
        //testCountDownLatch();
        //testCyclicBarrier();
        //testPhaser();
        //testPhaser2();
        //testReadWriteLock();
        //testSemaphore();
        //testExchanger();
        //testLockSupport();
        testLockSupport();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值