Semaphore 可以维护当前访问自身的线程个数,并提供了同步机制。使用Semaphore可以控制同时访问资源的线程个数,例如,实现一个文件允许的并发访问数。
单个信号量的Semaphore对象可以实现互斥锁的功能,并且可以是由一个线程获得了“锁”,再由另一个线程释放“锁”,这可应用于死锁恢复的一些场合。
package com.javase.thread;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
/**
* 10个线程并发访问牌数为3的信号灯
* @author Alex Zhuang
*
*/
public class SemaphoreDemo {
public static void main(String[] args) {
final Semaphore semaphore = new Semaphore(3);
ExecutorService threadPool = Executors.newCachedThreadPool();
for(int i=0;i<10;i++){
Runnable command =new Runnable(){
public void run() {
System.out.println(Thread.currentThread().getName()+" is waiting now");
try {
// Acquires a permit from this semaphore, blocking until one is available, or the thread is interrupted.
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" is running now");
System.out.println("There are "+ (3-semaphore.availablePermits()) + " thread got the permit of semaphore now");
try {
//Java SE5 引入的更加显示的sleep()版本,这个方法允许指定sleep()延迟的时间单元,因此有更好的可读性
TimeUnit.MILLISECONDS.sleep(new Random().nextInt(10000));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Releases a permit, returning it to the semaphore.
semaphore.release();
System.out.println(Thread.currentThread().getName()+" has been end now");
System.out.println("There are "+ (3-semaphore.availablePermits()) + " thread got the permit of semaphore now");
}
};
threadPool.execute(command);
}
threadPool.shutdown();
}
}