Java多线程并发JUC包ReentrantLock显示锁的用法

 更新时间:2025年05月16日 10:38:26   作者:二六八  
这篇文章主要介绍了Java多线程并发JUC包ReentrantLock显示锁的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

Java多线程并发JUC包 ReentrantLock 显示锁

ReentrantLock支持以下功能:

  • 支持公平和非公平的获取锁的方式。
  • 支持可重入。

公平锁与非公平锁:

  • 公平锁:加锁前先查看是否有排队等待的线程,有的话优先处理排在前面的线程,先来先得。
  • 非公平锁:线程加锁时直接尝试获取锁,获取不到就自动到队尾等待。

1 lock 和 unlock 方法说明

该demo模拟电影院的售票情况,tickets总票数。开启了10个窗口售票,售完为止,程序代码如下:

public class ReentrantLockDemo01 implements Runnable {

    private Lock lock = new ReentrantLock();

    private int tickets = 50;

    @Override
    public void run() {
        while (true) {
            // 获取锁
            if (lock.tryLock()) {
                try {
                    if (tickets > 0) {
                        TimeUnit.MILLISECONDS.sleep(100);
                        System.out.println(Thread.currentThread().getName() + " " + tickets--);
                    } else {
                        break;
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock(); // 释放所
                }
            }

        }
    }

    public static void main(String[] args) {
        ReentrantLockDemo01 reentrantLockDemo = new ReentrantLockDemo01();
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(reentrantLockDemo, "thread - " + i);
            thread.start();
        }
    }
}

输出如下 :

thread - 0 50
thread - 7 49
thread - 4 48
thread - 7 47
thread - 7 46
thread - 7 45
thread - 7 44
thread - 7 43
thread - 7 42
thread - 7 41
thread - 7 40
thread - 7 39
thread - 7 38
thread - 7 37
thread - 7 36
thread - 7 35
thread - 7 34
thread - 7 33
thread - 7 32
thread - 7 31
thread - 7 30
thread - 5 29
thread - 5 28
thread - 5 27
thread - 6 26
thread - 6 25
thread - 7 24
thread - 7 23
thread - 7 22
thread - 7 21
thread - 5 20
thread - 5 19
thread - 5 18
thread - 7 17
thread - 2 16
thread - 2 15
thread - 2 14
thread - 2 13
thread - 1 12
thread - 1 11
thread - 1 10
thread - 1 9
thread - 1 8
thread - 1 7
thread - 1 6
thread - 1 5
thread - 1 4
thread - 1 3
thread - 1 2
thread - 1 1

2 newCondition方法

Condition的作用是对锁进行更精确的控制。

Condition中的 await() 方法相当于Object的 wait() 方法,Condition中的 signal() 方法相当于Object的 notify() 方法,Condition中的 signalAll() 相当于Object的 notifyAll() 方法。

不同的是,Object中的 wait() , notify() , notifyAll() 方法是和”同步锁”(synchronized关键字)捆绑使用的;而Condition是需要与”互斥锁”/”共享锁”捆绑使用的。

/**
 * 生产者消费者
 */
public class ProducerConsumerTest {

    private Lock lock = new ReentrantLock();

    private Condition addCondition = lock.newCondition();

    private Condition removeCondition = lock.newCondition();

    private LinkedList<Integer> resources = new LinkedList<>();

    private int maxSize;

    public ProducerConsumerTest(int maxSize) {
        this.maxSize = maxSize;
    }


    public class Producer implements Runnable {

        private int proSize;

        private Producer(int proSize) {
            this.proSize = proSize;
        }

        @Override
        public void run() {
            lock.lock();
            try {
                for (int i = 1; i < proSize; i++) {
                    while (resources.size() >= maxSize) {
                        System.out.println("当前仓库已满,等待消费...");
                        try {
                            addCondition.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("已经生产产品数: " + i + "\t现仓储量总量:" + resources.size());
                    resources.add(i);
                    removeCondition.signal();
                }
            } finally {
                lock.unlock();
            }

        }
    }

    public class Consumer implements Runnable {

        @Override
        public void run() {
            String threadName = Thread.currentThread().getName();
            while (true) {
                lock.lock();
                try {
                    while (resources.size() <= 0) {
                        System.out.println(threadName + " 当前仓库没有产品,请稍等...");
                        try {
                            // 进入阻塞状态
                            removeCondition.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    // 消费数据
                    int size = resources.size();
                    for (int i = 0; i < size; i++) {
                        Integer remove = resources.remove();
                        System.out.println(threadName + " 当前消费产品编号为:" + remove);
                    }
                    // 唤醒生产者
                    addCondition.signal();
                } finally {
                    lock.unlock();
                }
            }

        }
    }

    public static void main(String[] args) throws InterruptedException {
        ProducerConsumerTest producerConsumerTest = new ProducerConsumerTest(10);
        Producer producer = producerConsumerTest.new Producer(100);
        Consumer consumer = producerConsumerTest.new Consumer();
        final Thread producerThread = new Thread(producer, "producer");
        final Thread consumerThread = new Thread(consumer, "consumer");
        producerThread.start();
        TimeUnit.SECONDS.sleep(2);
        consumerThread.start();
    }
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 使用EasyPoi完成复杂一对多excel表格导出功能全过程

    使用EasyPoi完成复杂一对多excel表格导出功能全过程

    这篇文章主要介绍了使用EasyPoi完成复杂一对多excel表格导出功能全过程,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • 使用SpringBoot配置虚拟化路径用于图片的展示

    使用SpringBoot配置虚拟化路径用于图片的展示

    这篇文章主要介绍了使用SpringBoot配置虚拟化路径用于图片的展示方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • Myeclipse工程发布时端口占用问题的解决方法

    Myeclipse工程发布时端口占用问题的解决方法

    这篇文章主要介绍了Myeclipse工程发布时端口占用问题的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • java通过DelayQueue实现延时任务

    java通过DelayQueue实现延时任务

    本文主要介绍了java通过DelayQueue实现延时任务,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • SpringSecurity 用户帐号已被锁定的问题及解决方法

    SpringSecurity 用户帐号已被锁定的问题及解决方法

    这篇文章主要介绍了SpringSecurity 用户帐号已被锁定,本文给大家分享问题原因及解决方式,需要的朋友可以参考下
    2023-12-12
  • java switch语句使用注意的四大细节

    java switch语句使用注意的四大细节

    很多朋友在使用java switch语句时,可能没有注意到一些细节,本文将详细介绍使用java switch语句四大要点,需要的朋友可以参考下
    2012-12-12
  • Java根据Request获取客户端IP

    Java根据Request获取客户端IP

    这篇文章主要介绍了Java根据Request获取客户端IP的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-05-05
  • Java中的@Accessors使用详解

    Java中的@Accessors使用详解

    这篇文章主要介绍了Java中的@Accessors使用详解,@RequiredArgsConstructor是Lombok的一个注解,简化了我们对setter和getter方法操作,它可以作用在类上,也可以作用在类的单个属性上,需要的朋友可以参考下
    2024-01-01
  • 如何使用@AllArgsConstructor和final 代替 @Autowired

    如何使用@AllArgsConstructor和final 代替 @Autowired

    这篇文章主要介绍了使用@AllArgsConstructor和final 代替 @Autowired方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • 详解Reactor如何优雅Exception异常处理

    详解Reactor如何优雅Exception异常处理

    初识响应式编程的时候,除了从命令式的思维方式转变为函数式的编程方式外,其中有一个很大的不适应的地方就是在面对异常时该怎么处理。本文将通过Project Reactor的文档以及源码来深入解读,在reactor中是如何优雅地实现这异常处理三板斧,希望对大家有所帮助
    2023-02-02

最新评论