Java并发之同步与锁

本文探讨了Java中Concurrent包的ReentrantLock和Condition类在多线程中的应用,通过实例解析了test1(), test2(), test3()方法的工作原理,重点讲解了锁的获取、释放、竞争以及Condition的使用。
摘要由CSDN通过智能技术生成
public class Concurrent3 {

    static Object obj = new Object();
    static Lock lock = new ReentrantLock();

    public static void main(String[] args) {
        for(int i = 0 ; i < 10 ; i++){
            test1();
        }

    }

    private static void test1() {

        Thread t = new Thread(() -> {
           synchronized (obj){
               try {
                   System.out.println("hello");
                   Thread.sleep(1000);
               }catch (Exception e){
                   e.printStackTrace();
               }
           }
        });
        t.start();
    }

    public static void test2(){
        Thread t = new Thread(() -> {

            try {
                lock.lock();
                System.out.println("hello");
                Thread.sleep(1000);
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                   lock.unlock();
            }
        });
        t.start();
    }
    
}

运行test1()和test2()
都是每隔一秒打印一句Hello
在这里插入图片描述
lock.tryLock();表示尝试获取锁
成功获取的话返回true
获取失败返回false

public static void test3(){
        Thread t = new Thread(() -> {
            boolean b = false;
            try {
                b = lock.tryLock();
                if(b){
                    System.out.println("hello");
                    Thread.sleep(1000);
                }

            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if(b)   lock.unlock();
            }
        });
        t.start();
    }

在这里插入图片描述
原因:线程1获取了锁成功,b==true
其他线程获取锁失败所以就没有执行输出语句

   public static void test3(){
        Thread t = new Thread(() -> {
            boolean b = false;
            try {

                b = lock.tryLock(1500L, TimeUnit.MILLISECONDS);
                if(b){
                    System.out.println("hello");
                    Thread.sleep(1000);
                }

            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if(b)   lock.unlock();
            }
        });
        t.start();
    }

在这里插入图片描述
在这里插入图片描述

obj.wait();//表示释放锁,

obj.wait():表示拿了锁然后把他释放掉,进入阻塞,等待被唤醒
obj.notify():唤醒一个调用wait()方法进入阻塞的线程,前提是你也占用了锁资源

static Object obj = new Object();
    static Lock lock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {
        for(int i = 0 ; i < 10 ; i++){
            test1();
        }
        Thread.sleep(2000);
        obj.notify();
    }

    private static void test1() {

        Thread t = new Thread(() -> {
           synchronized (obj){
               try {
                   obj.wait();
                   System.out.println("hello");
                   Thread.sleep(1000);
               }catch (Exception e){
                   e.printStackTrace();
               }
           }
        });
        t.start();
    }

    public static void test2(){
        Thread t = new Thread(() -> {

            try {
                lock.lock();
                System.out.println("hello");
                Thread.sleep(1000);
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                   lock.unlock();
            }
        });
        t.start();
    }

执行会报错
在这里插入图片描述
修改:

static Object obj = new Object();
    static Lock lock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {
        for(int i = 0 ; i < 10 ; i++){
            test1();
        }
        Thread.sleep(2000);
        synchronized (obj){
            obj.notify();
        }

    }

    private static void test1() {

        Thread t = new Thread(() -> {
           synchronized (obj){
               try {
                   obj.wait();
                   System.out.println("hello");
                   Thread.sleep(1000);
               }catch (Exception e){
                   e.printStackTrace();
               }
           }
        });
        t.start();
    }

    public static void test2(){
        Thread t = new Thread(() -> {

            try {
                lock.lock();
                System.out.println("hello");
                Thread.sleep(1000);
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                   lock.unlock();
            }
        });
        t.start();
    }

在这里插入图片描述
唤醒了一个阻塞线程,其他都在阻塞
obj.notifyAll();
唤醒所以

static Object obj = new Object();
    static Lock lock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {
        for(int i = 0 ; i < 10 ; i++){
            test1();
        }
        Thread.sleep(2000);
        synchronized (obj){
            obj.notifyAll();
        }

    }

    private static void test1() {

        Thread t = new Thread(() -> {
           synchronized (obj){
               try {
                   obj.wait();
                   System.out.println("hello");
                   Thread.sleep(1000);
               }catch (Exception e){
                   e.printStackTrace();
               }
           }
        });
        t.start();
    }

lock如何释放和唤醒锁

static Lock lock = new ReentrantLock();
Condition c1 = lock.newCondition();

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值