CountDownLatch:閉鎖
public class TestHarness
{
public long timeTasks(int nThread,final Runnable task) throws InterruptedException{
//開始開關
final CountDownLatch startGate = new CountDownLatch(1);
final CountDownLatch endGate = new CountDownLatch(nThread);
for (int i = 0; i < nThread; i++)
{
Thread t = new Thread(){
public void run(){
try
{
//等待開關打開
startGate.await();
task.run();
}
catch (Exception e)
{
// TODO: handle exception
}finally{
//任務完成,endGate-1
endGate.countDown();
}
}
};
t.start();
}
long start = System.nanoTime();
//開關打開,所有線程同一起跑線開始執行
startGate.countDown();
//等待所有線程執行完成
endGate.await();
long end = System.nanoTime();
//返回時間
return end-start;
}
}
Exchanger---兩個線程間數據交換
public static void main(String[] args)
{
final Exchanger<List<Integer>> exchanger = new Exchanger<List<Integer>>();
new Thread()
{
@Override
public void run()
{
List<Integer> l = new ArrayList<Integer>(2);
l.add(1);
l.add(2);
try
{
l=exchanger.exchange(l);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("thread1:"+Thread.currentThread().getName()+":"+l);
}
}.start();
new Thread()
{
@Override
public void run()
{
List<Integer> l = new ArrayList<Integer>(2);
l.add(4);
l.add(5);
try
{
l = exchanger.exchange(l);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("thread2:"+Thread.currentThread().getName()+":"+l);
}
}.start();
}
Semaphore:信號量
public class BounderHashSet<T>
{
private final Set<T> set;
private final Semaphore sem;
public BounderHashSet(int bound){
//構造函數中,初始化set爲同步容器類
this.set = Collections.synchronizedSet(new HashSet<T>());
//初始化容器的大小,用sem來控制容器
sem = new Semaphore(bound);
}
public boolean add(T o) throws InterruptedException{
//獲取許可,其實就是表明,容器的可用空間減1
sem.acquire();
boolean wasAdded = false;
try
{
wasAdded = set.add(o);
return wasAdded;
}finally{
if (!wasAdded)
{
//如果添加不成功,釋放許可
sem.release();
}
}
}
public boolean remove(Object o){
boolean wasRemoved = set.remove(o);
if (wasRemoved)
{
//釋放許可,可用空間+1
sem.release();
}
return wasRemoved;
}
}
實現一個有界隊列:
public class BounderBuffer<E>
{
//可用的元素
private final Semaphore availableItems;
//可用的空閒空間
private final Semaphore availableSpaces;
//容器底層爲數組
private final E[] items;
private int putPosition = 0;
private int takePosition = 0;
public BounderBuffer(int capacity){
//初始可用數據爲0
availableItems = new Semaphore(0);
//空閒空間數量
availableSpaces = new Semaphore(capacity);
//初始化數組
items = (E[])new Object[capacity];
}
public boolean isEmpty(){
return availableItems.availablePermits()==0;
}
public boolean isFull(){
return availableSpaces.availablePermits()==0;
}
public void put(E x) throws InterruptedException{
availableSpaces.acquire();
//doInsert
doInsert(x);
availableItems.release();
}
public E take() throws InterruptedException{
availableItems.acquire();
//doExtract
E x = doExtract();
availableSpaces.release();
return x;
}
private synchronized void doInsert(E x){
int i = putPosition;
items[i]=x;
putPosition = ++i%items.length;
}
private synchronized E doExtract(){
int i = takePosition;
E x = items[i];
items[i]=null;
takePosition = ++i%items.length;
return x;
}
}