Java多线程

http://stackoverflow.com/questions/10684111/can-notify-wake-up-the-same-thread-multiple-times

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Condition.html

Difference between Binary Semaphore and Mutex

http://stackoverflow.com/questions/62814/difference-between-binary-semaphore-and-mutex

Java并发之读写锁Lock和条件阻塞Condition的应用(转载)

http://www.cnblogs.com/yaowukonga/archive/2012/08/27/2658329.html

 

H2O与Java线程同步

http://www.cnblogs.com/lautsie/p/3430356.html

http://ttianzhao.blogspot.com/2014/07/concurrent-h2o.html

 

A Java example of bounded buffer

http://www.cnblogs.com/yaowukonga/archive/2012/08/27/2658329.html

public class BoundedBuffer{

final Lock lock = new ReentrantLock();

final Condition notFull = lock.newCondition();

final Condition notEmpty = lock.newCondition();

 

final Object [] items = new Object[100];

int putptr, takeptr, count;

public void put(Object x) throws InterruptedException{

lock.lock();

try{

while(count == items.length)  notFull.await();

items[putptr] = x ;

if( ++putptr == items.length)  putptr=  0 ;

++count;

notEmpty.signal();

} finally{

lock.unlock();

}

}

public Object take() throws InterruptedException{

lock.lock();

try{

while ( count == 0 )   notEmpty.await();

Object x = items[takeptr];

if( ++takeptr == items.length)  takeptr=  0;

— count ;

notFull.signal();

return x ;

}finally{

lock.unlock();

}

}

}

http://tutorials.jenkov.com/java-concurrency/blocking-queues.html

public class BlockingQueue{
private List queue = new LinkedList();
private int limit = 10 ;
public BlockingQueue(int limit){
this.limit = limit ;
}

public synchronized void enqueue(Object item) throws InterruptedException{
while(this.queue.size() == this.limit){
wait();
}
if(this.queue.size() == 0){
notifyAll();
}
this.queue.add(item) ;
}

public synchronized void deque() throws InterruptedException {
while(this.queue.size() == 0 ) {
wait;
}
if( this.queue.size() == this.limit) {
notifyAll();
}
return this.queue.remove(0);

}

}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s