千家信息网

Java并发编程中的Semaphore是什么意思

发表于:2024-10-18 作者:千家信息网编辑
千家信息网最后更新 2024年10月18日,这篇文章将为大家详细讲解有关Java并发编程中的Semaphore是什么意思,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。简介Semaphore是用来限制访问特定资
千家信息网最后更新 2024年10月18日Java并发编程中的Semaphore是什么意思

这篇文章将为大家详细讲解有关Java并发编程中的Semaphore是什么意思,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

    简介

    Semaphore是用来限制访问特定资源的并发线程的数量,相对于内置锁synchronized和重入锁ReentrantLock的互斥性来说,Semaphore可以允许多个线程同时访问共享资源。

    Semaphored的使用

    构造方法

    Semaphore(int permits):创建Semaphore,并指定许可证的数量。(公平策略为非公平)

    Semaphore(int permits, boolean fair):创建Semaphore,并指定许可证的数量和公平策略。

    核心方法

    acquire():从Semaphore中获取一个许可证,如果获取不到则阻塞等待,直到其他线程释放了一个许可证或者当前线程被中断。

    acquire(int permits):从Semaphore中获取指定数量的许可证,如果获取不到则阻塞等待,直到其他线程释放了对应数量的许可证或者当前线程被中断。

    acquireUninterruptibly():从Semaphore中获取一个许可证,如果获取不到则阻塞等待,直到其他线程释放了一个许可证。(不响应中断)

    tryAcquire():尝试从Semaphore中获取一个许可证,获取成功则返回true,获取失败则返回false,不会进行等待。(不受公平策略的影响,许可证可用则立即获得)

    tryAcquire(long timeout, TimeUnit unit):尝试从Semaphore中获取一个许可证,获取成功则返回true,获取失败则等待指定的时间,直到等待时间结束还是没有获取到许可证则返回false。

    release():释放一个许可证。

    release(int permits):释放指定数量的许可证。

    示例

    总共有5个许可证,最先获取到许可证的5个线程开始执行任务,没获取到的线程进入等待状态,直到获取到许可证的线程释放许可证后,再获取许可证执行任务。

    public class Demo {    public static void main(String[] args) {        //创建许可证数量为5的Semaphore        Semaphore semaphore = new Semaphore(5);        Runnable runnable = () -> {            String threadName = Thread.currentThread().getName();            try{                //获取一个许可证                semaphore.acquire();                System.out.println(threadName + "执行任务...");                Thread.sleep(3000);            } catch (InterruptedException e) {                e.printStackTrace();            } finally {                //释放一个许可证                semaphore.release();            }        };        ExecutorService executorService = Executors.newFixedThreadPool(10);        for(int i = 0; i < 10; i++){            executorService.execute(runnable);        }        executorService.shutdown();    }}/* 开始输出: * pool-1-thread-1执行任务... * pool-1-thread-5执行任务... * pool-1-thread-6执行任务... * pool-1-thread-7执行任务... * pool-1-thread-3执行任务... * 三秒后输出: * pool-1-thread-4执行任务... * pool-1-thread-8执行任务... * pool-1-thread-2执行任务... * pool-1-thread-10执行任务... * pool-1-thread-9执行任务... */

    使用Semaphore实现互斥

    使用Semaphore实现互斥只需要将许可证数量设置为1,这样就可以保证只有一个线程能获取到许可证。

    Semaphore semaphore = new Semaphore(1);

    相比内置锁synchronized和重入锁ReentrantLock,使用Semaphore实现互斥有个明显的缺点:不可重入,没有释放许可证的情况下,再次调acquire方法将导致死锁。

    示例:

    public class Demo {    public static void main(String[] args) {        Semaphore semaphore = new Semaphore(1);        Runnable runnable = () -> {            String threadName = Thread.currentThread().getName();            try {                //获取一个许可证                semaphore.acquire();                System.out.println(threadName + "执行任务A...");                semaphore.acquire();                System.out.println(threadName + "执行任务B...");            } catch (InterruptedException e) {                e.printStackTrace();            } finally {                //释放一个许可证                semaphore.release();            }        };        new Thread(runnable).start();    }}/* * 输出结果: * Thread-0执行任务A... */

    "执行任务B"永远不会打印,因为许可证只有一个,第二次acquire方法的调用会因为无法获取到许可证而一直阻塞。

    关于"Java并发编程中的Semaphore是什么意思"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

    0