Java多线程之Semephore

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();

	}

}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值