首页 > 代码库 > 允许多个线程同时访问:信号量(读书笔记)

允许多个线程同时访问:信号量(读书笔记)

 信号量为多线程协作提供了更为强大的控制方法,广义上说,信号量是对锁的拓展,无论是内部锁synchronized还是重入锁ReentrantLock,一次都只允许一个线程访问一个资源,而信号量缺可以指定多个线程.同时访问某一个资源,信号量主要提供了一下构造函数:
public Semaphore(int permits)
public Semaphore(int permits, boolean fair)//第二个参数可以指定是否公平
     在构造信号量对象时,必须要指定信号量的准入数,就是同时能申请多少个许可,当每个线程每次只申请一个许可时,这就相当于指定了同时有多少个线程可以访问某一个资源,
public void acquire()//尝试获得一个准入的许可,若无法获得,等待直到有线程释放一个或者中断.
public void acquireUninterruptibly()//和acquire()差不多,只是不响应中断,
public boolean tryAcquire()//试图去获取一个许可,成功立即返回true 失败立即false
public boolean tryAcquire(long timeout, TimeUnit unit)//规定一个等待时间,超时就false
public void release()//释放一个许可.
下面是一个比较傻瓜的例子:
public class SemapDemo implements Runnable {
    final Semaphore semp = new Semaphore(5);//允许五个许可

    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object‘s
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see Thread#run()
     */
    @Override
    public void run() {
        try {
            semp.acquire();
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getId() + ":done!");
            semp.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        ExecutorService exec = Executors.newFixedThreadPool(20);//容量为20的线程池
        final SemapDemo demo = new SemapDemo();
        for (int i = 0; i < 20; i++) {
            exec.submit(demo);
        }

    }
}

 

 

允许多个线程同时访问:信号量(读书笔记)