标签:
转载于:http://janeky.iteye.com/blog/769965
我们先来学习一下JDK1.5 API中关于这个类的详细介绍:
“一个计数信号量。从概念上讲,信号量维护了一个许可集。如有必要,在许可可用前会阻塞每一个 acquire(),然后再获取该许可。每个 release() 添加一个许可,从而可能释放一个正在阻塞的获取者。但是,不使用实际的许可对象,Semaphore 只对可用许可的号码进行计数,并采取相应的行动。”
我们一般用它来控制某个对象的线程访问对象
例如,对于某个容器,我们规定,最多只能容纳n个线程同时操作
使用信号量来模拟实现
package com.winterbe.java8.samples.concurrent;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
/*
* Semaphore通常用于对象池的控制
* */
public class TestSemaphore {
public static void main(String[] args) throws InterruptedException {
ExecutorService exec = Executors.newCachedThreadPool();
TestSemaphore t = new TestSemaphore();
final BoundedHashSet<String> set = t.getSet();
final int MAX=10;
for (int i = 0; i < MAX; i++) {// 十个线程同时操作add
exec.execute(new Runnable() {
public void run() {
try {
set.deal(Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
exec.shutdown();
}
public BoundedHashSet<String> getSet() {
return new BoundedHashSet<String>(2);// 定义一个边界约束为2的线程
}
class BoundedHashSet<T> {
private final Set<T> set;
private final Semaphore semaphore;
final Random random = new Random();
public BoundedHashSet(int bound) {
this.set = Collections.synchronizedSet(new HashSet<T>());
this.semaphore = new Semaphore(bound, true);
}
public void deal(T o) throws InterruptedException {
System.out.println("availablePermits:"+semaphore.availablePermits());
semaphore.acquire();// 信号量控制可访问的线程数目,如果访问的数量达到Bount,则阻塞
set.add(o);
System.out.printf("add:%s%n", o);
Thread.sleep(random.nextInt(1000));//Sleep 一定时间后,师傅信号量,这里也就是业务逻辑处理的时间
semaphore.release();// 释放掉信号量
System.out.printf("remove:%s%n", o);
}
}
}
总结:Semaphore通常用于对象池的控制
java多线程学习-java.util.concurrent详解(三) Semaphore
标签:
原文地址:http://www.cnblogs.com/fanxiaojun/p/4914508.html