new Semaphore(0)关于信号量的笔记

解释

Semaphore(信号量):是一种计数器,用来保护一个或者多个共享资源的访问。如果线程要访问一个资源就必须先获得信号量。如果信号量内部计数器大于0,信号量减1,然后允许共享这个资源;否则,如果信号量的计数器等于0,信号量将会把线程置入休眠直至计数器大于0.当信号量使用完时,必须释放。

主要方法:

  • void acquire() :从信号量获取一个许可,如果无可用许可前将一直阻塞等待,
  • void acquire(int permits) :获取指定数目的许可,如果无可用许可前也将会一直阻塞等待
  • boolean tryAcquire():从信号量尝试获取一个许可,如果无可用许可,直接返回false,不会阻塞
  • boolean tryAcquire(int permits): 尝试获取指定数目的许可,如果无可用许可直接返回false
  • boolean tryAcquire(int permits, long timeout, TimeUnit unit): 在指定的时间内尝试从信号量中获取许可,如果在指定的时间内获取成功,返回true,否则返回false
  • void release(): 释放一个许可,别忘了在finally中使用,注意:多次调用该方法,会使信号量的许可数增加,达到动态扩展的效果,如:初始permits为1, 调用了两次release,最大许可会改变为2
  • int availablePermits(): 获取当前信号量可用的许可

自己总结的

做算法题,按序执行,使用信号量的方式,使用了new Semaphore(0),之前不明白,后来了解到,当信号量为0的时候,在线程使用了acquire()去请求信号量,结果就会导致整个程序阻塞了。原来是调用了acquire()相当于是信号量从0,减去1变成-1,此时如果没有执行release,那么线程就会阻塞在这里。使用release会使信号量+1。此时才不会阻塞。

算法题如下

我们提供了一个类:

public class Foo {
  public void first() { print("first"); }
  public void second() { print("second"); }
  public void third() { print("third"); }
}
三个不同的线程 A、B、C 将会共用一个 Foo 实例。

一个将会调用 first() 方法
一个将会调用 second() 方法
还有一个将会调用 third() 方法
请设计修改程序,以确保 second() 方法在 first() 方法之后被执行,third() 方法在 second() 方法之后被执行。

写的算法如下

import java.util.concurrent.Semaphore;
class Foo {
    public Semaphore seam_first_two = new Semaphore(0);
    
    public Semaphore seam_two_second = new Semaphore(0);
    
    public Foo() {
        
    }

    public void first(Runnable printFirst) throws InterruptedException {
        printFirst.run();
        seam_first_two.release();//先使信号量+1,才能执行后面的acquire
    }

    public void second(Runnable printSecond) throws InterruptedException {
        seam_first_two.acquire();//先执行first后再执行这个
        printSecond.run();
        seam_two_second.release();
    }

    public void third(Runnable printThird) throws InterruptedException {
        seam_two_second.acquire();
        printThird.run();
    }
}
posted @ 2021-11-18 20:46  Honglixi  阅读(984)  评论(0)    收藏  举报