zoukankan      html  css  js  c++  java
  • 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方法的调用会因为无法获取到许可证而一直阻塞。

  • 相关阅读:
    hdu6229 Wandering Robots 2017沈阳区域赛M题 思维加map
    hdu6223 Infinite Fraction Path 2017沈阳区域赛G题 bfs加剪枝(好题)
    hdu6438 Buy and Resell 买卖物品 ccpc网络赛 贪心
    hdu6441 Find Integer 求勾股数 费马大定理
    bzoj 1176 Mokia
    luogu 3415 祭坛
    bzoj 1010 玩具装箱
    bzoj 3312 No Change
    luogu 3383【模板】线性筛素数
    bzoj 1067 降雨量
  • 原文地址:https://www.cnblogs.com/seve/p/14644206.html
Copyright © 2011-2022 走看看