java常用锁

本文展示了Java中几种并发控制工具的使用,包括自定义的自旋锁实现,CountDownLatch用于计数事件的发生,CyclicBarrier用于同步多个线程,以及Semaphore进行资源限制的场景。通过实例代码演示了它们在多线程环境中的应用。
摘要由CSDN通过智能技术生成

自实现的线程自旋锁
在这里插入图片描述
在这里插入图片描述
读写锁
写锁
在这里插入图片描述
读锁
在这里插入图片描述
CountDownLatch做加法
在这里插入图片描述
CylicBarrierDemo做减法
在这里插入图片描述
Semaphore 抢车位
在这里插入图片描述
代码

package com.ge.healthcare.cn.apm.gateway.util;
import java.util.concurrent.*;import java.util.concurrent.atomic.AtomicReference;
public class SpinLockDemo {
    AtomicReference<Thread> atomicReference = new AtomicReference<>();    
    public void myLock(){
        Thread thread = Thread.currentThread();        
        System.out.println(Thread.currentThread().getName()+"come in");        
        while (!atomicReference.compareAndSet(null,thread)){}
    }

    public void myUnlock(){
        Thread thread = Thread.currentThread();        
        atomicReference.compareAndSet(thread,null);        
        System.out.println(Thread.currentThread().getName()+" invoked myUnLock()");    
    }

    public static void main(String[] args) {
        //自己实现的自旋锁        
        SpinLockDemo spinLockDemo = new SpinLockDemo();        
        new Thread(()->{
            spinLockDemo.myLock();            
            System.out.println(Thread.currentThread().getName()+"正在干活");            
            try{TimeUnit.SECONDS.sleep(10);}catch (InterruptedException e){e.printStackTrace();}
            spinLockDemo.myUnlock();        
        },"AA").start();
        try{TimeUnit.SECONDS.sleep(2);}catch (InterruptedException e){e.printStackTrace();}

        new Thread(()->{
            spinLockDemo.myLock();            
            System.out.println(Thread.currentThread().getName()+"正在干活");            
            spinLockDemo.myUnlock();        
        },"BB").start();
        //CountDownLatch        
        CountDownLatch countDownLatch = new CountDownLatch(6);        
        for (int i = 0; i <=6 ; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"上完自习离开教室");                
                countDownLatch.countDown();            
            },String.valueOf(i)).start();        }
        try {countDownLatch.await();} catch (InterruptedException e) {e.printStackTrace();}
        System.out.println(Thread.currentThread().getName()+"————————最后关门");
        //CylicBarrierDemo做减法        
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
            System.out.println("集齐");        
        });        
        for (int i = 1; i <=7 ; i++) {
            final int tempInt = i;            
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"收集到第:"+tempInt+"个");                try {
                    cyclicBarrier.await();                
                } catch (InterruptedException e) {
                    e.printStackTrace();                
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();                
                }
            },String.valueOf(i)).start();
        }

        //Semaphore抢车位        
        Semaphore semaphore = new Semaphore(3);        
        for (int i = 1; i <=6 ; i++) {
            new Thread(()->{
                try {
                    semaphore.acquire();                    
                    System.out.println(Thread.currentThread().getName()+"抢到");                    
                    try{TimeUnit.SECONDS.sleep(3);}catch (InterruptedException e){e.printStackTrace();}
                    System.out.println(Thread.currentThread().getName()+"离开");                
                } catch (InterruptedException e) {
                    e.printStackTrace();                
                }finally {
                    semaphore.release();                
                }
            },String.valueOf(i)).start();
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值