并发—Semaphore、CountDownLatch、CyclicBarrier的使用

Semaphore

和 synchronized、ReentrantLock 不同的是,Semaphore 允许多个线程访问同一个资源,常常用作限制对一个资源的访问线程数。

Semaphore使用,模拟公交车

import java.util.concurrent.Semaphore;
/**
 * 模拟公交车(火车)
 * 信号量: 信号量维护了一些许可证,每一个 acquire() 方法在获取到许可证之前,如果当前没有可用的许可  证,就会被阻塞,然后才能拿到许可证。 每个release() 方法添加一个许可证,潜在得释放一个被阻塞的获取者。但是,并没有实际的许可证,Semaphore 只是维护了可用许可证的数量。
 * 信号量常常用来限制访问某些资源(物理的或逻辑的)的线程数量
 * @author ljx
 * @Date Jan 16, 2019 10:34:51 AM
 */
public class Bus {
   

	private final static int SEAT_MAX = 5;

	private Semaphore tickets = new Semaphore(SEAT_MAX);
	private boolean[] used = new boolean[SEAT_MAX];

    // 上车买票,然后得到一个座位
	public int getSeat() throws InterruptedException {
   
		tickets.acquire();
		return getAviliableSeat();
	}
	// 下车
	public void debus(int i) {
   
		markAsUnused(i);
		tickets.release();
	}

	private synchronized int getAviliableSeat() {
   
		for (int i = 0; i < SEAT_MAX; i++) {
   
			if (!this.used[i]) {
   
				used[i] = true;
				return i + 1;
			}
		}
		return -1;
	}

	private synchronized void markAsUnused(int i) {
   
		this.used[i - 1] = false;
	}
}

执行:

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * 信号量: 信号量维护了一些许可证,每一个 acquire() 方法在获取到许可证之前,如果当前没有可用的许可证,就会被阻塞,然后才能拿到许可证。 每个
 * release() 方法添加一个许可证,潜在得释放一个被阻塞的获取者。但是,并没有实际的许可证,Semaphore 只是维护了可用许可证的数量。
 * 
 * 信号量常常用来限制访问某些资源(物理的或逻辑的)的线程数量
 * 
 * @author ljx
 * @Date Jan 16, 2019 9:58:47 AM
 *
 */
public class SemaphoreTest {
   

	static class Passenger implements Runnable {
   

		private Bus bus;
		private String name;

		public Passenger(Bus bus, String name) {
   
			this.bus = bus;
			this.name = name;
		}

		@Override
		public void run() {
   
			try {
   
				int seat = bus.getSeat();
				System.out.println("Passenger " + name + " get a seat: " + seat);
				Thread.sleep((int) (Math.random() * 5000));
				bus.debus(seat);
				System.out.println("           Passenger " + name + " debus,seat: " + seat + " is useable");
			} catch (InterruptedException e) {
   
				e.printStackTrace();
			}
		}

	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值