08:java基础-锁之AQS&ReentrantLock&BlockingQueue&CountDownLatch&Semapho

  • Semaphore 字面意思是信号量的意思,它的作用是控制访问特定资源的线程数目,底层依赖AQS的状态State,是在生产当中比较常用的一个工具类。

怎么使用 Semaphore?

构造方法
 public Semaphore(int permits)
 public Semaphore(int permits, boolean fair)

重要方法
public void acquire() throws InterruptedException
public void release()
tryAcquire(long timeout, TimeUnit unit)

基本使用

需求场景

  • 资源访问,服务限流(Hystrix里限流就有基于信号量方式)。
    代码实现
package com.zgs.lock.semapho;

import java.util.concurrent.Semaphore;

/\*\*
 \* @author guisong.zhang
 \* @date 2024/3/11 23:09
 \* @description semapho测试类
 \* 默认实现非公平锁
 \*/
public class SemaphoRunnerTest {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(2);
        for (int i = 0; i < 10; i++) {
            new Thread(new Task(semaphore, "张贵松-线程" + i)).start();
        }
    }

    static class Task extends Thread {
        Semaphore semaphore;

        public Task(Semaphore semaphore, String name) {
            super(name);
            this.semaphore = semaphore;
        }

        @Override
        public void run() {
            try {
                //获取资源
                semaphore.acquire();
                System.out.println(Thread.currentThread().getName() + "获取到资源,时间:" + System.currentTimeMillis());
                Thread.sleep(5000);
                //释放资源
                semaphore.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

  • 输出结果
    在这里插入图片描述
  • 从打印结果可以看出,一次只有两个线程执行 acquire(),只有线程进行 release() 方法后才会有别的线程执行 acquire()。

Semapho源码流程图

Semapho示例代码git地址

3:AQS应用之CountDownLatch

CountDownLatch是什么?

  • CountDownLatch这个类能够使一个线程等待其他线程完成各自的工作后再执行。例如,应用程序的主线程希望在负责启动框架服务的线程已经启动所有的框架服务之后再执行
  • 使用场景:Zookeeper分布式锁,Jmeter模拟高并发等

CountDownLatch如何工作?

  • CountDownLatch是通过一个计数器来实现的,计数器的初始值为线程的数量。每当一个线程完成了自己的任务后,计数器的值就会减1。
  • 当计数器值到达0时,它表示所有的线程已经完成了任务,然后在闭锁上等待的线程就可以恢复执行任务

API

CountDownLatch.countDown()
CountDownLatch.await();

CountDownLatch应用场景例子

  • 比如陪媳妇去看病。医院里边排队的人很多,如果一个人的话,要先看大夫,看完大夫再去排队交钱取药。现在我们是双核,可以同时做这两个事(多线程)。
  • 假设看大夫花3秒钟,排队交费取药花5秒钟。我们同时搞的话,5秒钟我们就能完成,然后一起回家(回到主线程)
代码如下:
  • CountDownLatchRunner
package com.zgs.lock.countdown\_latch;

import java.util.concurrent.CountDownLatch;

/\*\*
 \* @author: guisong.zhang
 \* @date: 2024/3/12 15:12:09
 \* @description CountDownLatch测试类
 \*\*/
public class CountDownLatchRunner {
    public static void main(String[] args) throws InterruptedException {
        long timeNow = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(2);

        new Thread(new SeeDoctorTask(countDownLatch)).start();
        new Thread(new QueueTask(countDownLatch)).start();

        countDownLatch.await();
        System.out.println("等待所有线程执行完毕后继续执行——cost time:" + (System.currentTimeMillis() - timeNow));
    }

}

  • QueueTask
package com.zgs.lock.countdown\_latch;

import java.util.concurrent.CountDownLatch;

/\*\*
 \* @author: guisong.zhang
 \* @date: 2024/3/12 15:27:57
 \* @description TODO
 \*\*/
public class QueueTask extends Thread {
    private CountDownLatch countDownLatch;

    public QueueTask(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        try {
            System.out.println("开始在医院药房排队买药....");
            Thread.sleep(5000);
            System.out.println("排队成功,可以开始缴费买药");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (countDownLatch != null) {
                countDownLatch.countDown();
            }
        }
    }
}

  • SeeDoctorTask
package com.zgs.lock.countdown\_latch;

import java.util.concurrent.CountDownLatch;

/\*\*
 \* @author: guisong.zhang
 \* @date: 2024/3/12 15:28:13
 \* @description TODO
 \*\*/
public class SeeDoctorTask extends Thread {
    private CountDownLatch countDownLatch;

    public SeeDoctorTask(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        try {
            System.out.println("开始看医生");
            Thread.sleep(3000);
            System.out.println("结束看医生");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != countDownLatch) {
                countDownLatch.countDown();
            }
        }
    }
}

CountDownLatch源码流程图

CountDownLatch示例代码git地址

4:CyclicBarrier

  • 栅栏屏障,让一组线程到达一个屏障(也可以叫同步点)时被阻塞,直到最后一个线程到达屏障时,屏障才会开门,所有被屏障拦截的线程才会继续运行。
  • CyclicBarrier默认的构造方法是CyclicBarrier(int parties),其参数表示屏障拦截的线程数量,每个线程调用await方法告CyclicBarrier我已经到达了屏障,然后当前线程被阻塞。

API

cyclicBarrier.await();

应用场景

  • 可以用于多线程计算数据,最后合并计算结果的场景。
  • 例如,用一个Excel保存了用户所有银行流水,每个Sheet保存一个账户近一年的每笔银行流水,现在需要统计用户的日均银行流水,
  • 先用多线程处理每个sheet里的银行流水,都执行完之后,得到每个sheet的日均银行流水,
  • 最后,再用barrierAction用这些线程的计算结果,计算出整个Excel的日均银行流水。

示例代码:

package com.zgs.lock.cyclicBarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/\*\*
 \* @author: guisong.zhang
 \* @date: 2024/3/12 16:52:33
 \* @description CyclicBarrierRunner测试类
 \*\*/

public class CyclicBarrierRunner {

    static class WorkerThread implements Runnable {
        private final CyclicBarrier barrier;
        private int id;

        public WorkerThread(CyclicBarrier barrier, int id) {
            this.barrier = barrier;
            this.id = id;
        }

        @Override
        public void run() {
            try {
                // 模拟线程做准备工作或任务执行
                System.out.println("Worker " + id + " started.");
                Thread.sleep(1000); // 假设执行耗时操作

                System.out.println("Worker " + id + " is about to reach the barrier.");

                // 当前线程到达屏障点并等待其他线程
                barrier.await();

                System.out.println("Worker " + id + " passed the barrier and can continue now.");
            } catch (InterruptedException | BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        int numberOfWorkers = 5; // 线程数量
        CyclicBarrier barrier = new CyclicBarrier(numberOfWorkers, () -> {
            System.out.println("All workers have reached the barrier. Executing a barrier action...");
        });

        ExecutorService executorService = Executors.newFixedThreadPool(numberOfWorkers);

        for (int i = 0; i < numberOfWorkers; i++) {
            executorService.execute(new WorkerThread(barrier, i + 1));
        }

        // 关闭线程池
        executorService.shutdown();
    }
}


CyclicBarrier示例代码git地址

5:AQS应用之BlockingQueue

  • BlockingQueue,是java.util.concurrent 包提供的用于解决并发生产者 消费者问题的最有用的类,它的特性是在任意时刻只有一个线程可以进行take或者put操作,并且BlockingQueue提供了超时return null的机制,在许多生产场景里都可以看到这个工具的身影。

队列类型

  1. 无限队列 (unbounded queue ) - 几乎可以无限增长
  2. 有限队列 ( bounded queue ) - 定义了最大容量

队列数据结构

队列实质就是一种存储数据的结构

  • 通常用链表或者数组实现
  • 一般而言队列具备FIFO先进先出的特性,当然也有双端队列(Deque)优先级队列
  • 主要操作:入队(EnQueue)与出队(Dequeue)
    在这里插入图片描述

常见的4种阻塞队列

  • ArrayBlockingQueue 由数组支持的有界队列
  • LinkedBlockingQueue 由链接节点支持的可选有界队列
  • PriorityBlockingQueue 由优先级堆支持的无界优先级队列
  • DelayQueue 由优先级堆支持的、基于时间的调度队列

ArrayBlockingQ

  • 队列基于数组实现,容量大小在创建ArrayBlockingQueue对象时已定义好
  • 数据结构如下图:
    在这里插入图片描述
  • 队列创建:
BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>();

  • 应用场景
    在线程池中有比较多的应用,生产者消费者场景
  • 工作原理
    基于ReentrantLock保证线程安全,根据Condition实现队列满时的阻塞

LinkedBlockingQueue

  • 是一个基于链表的无界队列(理论上有界)
BlockingQueue<String> blockingQueue = new LinkedBlockingQueue<>();

  • 上面这段代码中,blockingQueue 的容量将设置为 Integer.MAX_VALUE 。
  • 向无限队列添加元素的所有操作都将永远不会阻塞,[注意这里不是说不会加锁保证线程安全],因此它可以增长到非常大的容量。
  • 使用无限 BlockingQueue 设计生产者 - 消费者模型时最重要的是 消费者应该能够像生产者向队列添加消息一样快地消费消息 。否则,内存可能会填满,然后就会得到一个 OutOfMemory 异常。

DelayQueue

  • 由优先级堆支持的、基于时间的调度队列,内部基于无界队列PriorityQueue实现,而无界队列基于数组的扩容实现。
  • 队列创建
BlockingQueue<String> blockingQueue = new DelayQueue();

  • 要求
    入队的对象必须要实现Delayed接口,而Delayed集成自Comparable接口
  • 应用场景
    电影票
  • 工作原理:
    队列内部会根据时间优先级进行排序。延迟类线程池周期执行。

BlockingQueue API

  • BlockingQueue 接口的所有方法可以分为两大类:负责向队列添加元素的方法和检索这些元素的方法。在队列满/空的情况下,来自这两个组的每个方法的行为都不同。
添加元素

在这里插入图片描述

检索元素

在这里插入图片描述

在构建生产者 - 消费者程序时,这些方法是 BlockingQueue 接口中最重要的构建块。

多线程生产者-消费者示例
  • 代码说明
  • 接下来我们创建一个由两部分组成的程序 - 生产者 ( Producer ) 和消费者 ( Consumer) 。
  • 生产者将生成一个 0 到 100 的随机数(十全大补丸的编号),并将该数字放在BlockingQueue 中。我们将创建 16 个线程(潘金莲)用于生成随机数并使用 put() 方法阻塞,直到队列中有可用空间。
  • 需要记住的重要一点是,我们需要阻止我们的消费者线程无限期地等待元素出现在队列中。
  • 从生产者(潘金莲)向消费者(武大郎)发出信号的好方法是,不需要处理消息,而是发送称为毒( poison ) 丸 ( pill ) 的特殊消息。 我们需要发送尽可能多的毒 ( poison )丸 ( pill ) ,因为我们有消费者(武大郎)。然后当消费者从队列中获取特殊的毒 (poison ) 丸 ( pill )消息时,它将优雅地完成执行。
  • 以下生产者的代码:
package com.zgs.lock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadLocalRandom;

/\*\*
 \* @author: guisong.zhang
 \* @date: 2024/3/7 15:26:09
 \* @description TODO
 \*\*/
public class NumbersProducer implements Runnable {
    private BlockingQueue<Integer> numbersQueue;
    private final int poisonPill;
    private final int poisonPillPerProducer;

    public NumbersProducer(BlockingQueue<Integer> numbersQueue, int poisonPill, int poisonPillPerProducer) {
        this.numbersQueue = numbersQueue;
        this.poisonPill = poisonPill;
        this.poisonPillPerProducer = poisonPillPerProducer;
    }

    @Override
    public void run() {
        try {
            generateNumbers();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    private void generateNumbers() throws InterruptedException {
        for (int i = 0; i < 100; i++) {
            numbersQueue.put(ThreadLocalRandom.current().nextInt(100));
            System.out.println("潘金莲‐" + Thread.currentThread().getId() + "-号,给武大郎的泡药!");
        }
        for (int j = 0; j < poisonPillPerProducer; j++) {
            numbersQueue.put(poisonPill);
            System.out.println("潘金莲‐" + Thread.currentThread().getId() + "-号,往武大郎的药里放入第" + j + 1 + "颗毒丸!");
        }
    }
}

  • 我们的生成器构造函数将 BlockingQueue 作为参数,用于协调生产者和使用者之间的处理。我们看到方法 generateNumbers() 将 100 个元素(生产100副药给武大郎吃)放入队列中。
  • 它还需要有毒 ( poison ) 丸 ( pill ) (潘金莲给武大郎下毒)消息,以便知道在执行完成时放入队列的消息类型。该消息需要将 poisonPillPerProducer 次放入队列中。
  • 每个消费者将使用 take() 方法从 BlockingQueue 获取一个元素,因此它将阻塞,直到队列中有一个元素。从队列中取出一个 Integer 后,它会检查该消息是否是毒 ( poison) 丸 ( pill )(武大郎看潘金莲有没有下毒) ,
  • 如果是,则完成一个线程的执行。否则,它将在标准输出上打印出结果以及当前线程的名称。
package com.zgs.lock;

import java.util.concurrent.BlockingQueue;

/\*\*
 \* @author: guisong.zhang
 \* @date: 2024/3/7 15:30:16
 \* @description TODO
 \*\*/
public class NumbersConsumer implements Runnable {
    private BlockingQueue<Integer> queue;
    private final int poisonPill;

    public NumbersConsumer(BlockingQueue<Integer> queue, int poisonPill) {
        this.queue = queue;
        this.poisonPill = poisonPill;
    }

    public void run() {
        try {
            while (true) {
                Integer number = queue.take();
                if (number.equals(poisonPill)) {
                    return;
                }
                System.out.println("武大郎‐" + Thread.currentThread().getId() + "-号,喝药‐编号:" + number);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}

  • 需要注意的重要事项是队列的使用。与生成器构造函数中的相同,队列作为参数传递。我们可以这样做,是因为 BlockingQueue 可以在线程之间共享而无需任何显式同步。
  • 既然我们有生产者和消费者,我们就可以开始我们的计划。我们需要定义队列的容量,并将其设置为 10个元素
  • 我们创建4 个生产者线程,并且创建等于可用处理器数量的消费者线程:
package com.zgs.lock;

import java.util.concurrent.BlockingQueue;
  • 13
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值