70 likes | 340 Views
Semaphore. A special type of lock. Similar to lock as it can be used to prevent access to shared data when the data is locked. A lock that has a value. A semaphore value represents the number of “permits.”
E N D
Semaphore • A special type of lock. • Similar to lock as it can be used to prevent access to shared data when the data is locked. • A lock that has a value. • A semaphore value represents the number of “permits.” • Package java.util.concurrent;public class Semaphore{ public Semaphore(long permits) … }
Semaphore Value • public class Semaphore{ public Semaphore(long permits) public void acquire() public void acquire(long permits) public void release() public void release(long permits) • acquire() • grabs a permit and decrements the semaphore value (# of permits) • release() • returns a permit and increments the semaphore value (# of permits) • acquire() blocks • when the semaphore value (# of permits) becomes 0 • until a permit is available. • acquire() does not block • as far as the semaphore value is greater than 0 (as far as permits are available).
The semaphore value can represent • the number of locks that can be granted. • e.g., a semaphore can be used to simulate a reader-writer lock (with the max number of readers) by having • a reader acquire one permit, and • a writer acquire all permits. • the maximum number of objects in an object pool. • e.g., connection pooling • A pool of instantiated connection objects • Connections are recycled to send data (e.g., commands and queries) to servers (e.g., a web server and database) • No runtime overhead to dynamically instantiate connections • The max number of simultaneous connections is bounded.
Comparison with Lock • A semaphore is mostly same as a lock • if the max number of permits is 1. • Differences • No condition objects are available in a semaphore. • No newCondition() supported. • ReentrantLock has this method. • No nested locking in a semaphore • A semaphore can acquire multiple permits.
Barrier • A rendezvous point for multiple threads. • All threads meet (or are synchronized) at a rendezvous point • before any of them are permitted to pass the point.
Barrier and join() • What’s the differences between a barrier and join()? • When join() returns • a target thread exits • no way to access data stored in a corresponsing Runnable object. • A barrier provides access to data stored in a Runnable object • even after a corresponding thread exits.
CyclicBarrier • java.util.concurrent.CyclicBarrier • public class CyclicBarrier{ public CyclicBarrier( int parties ) public CyclicBarrier( int parties, Runnable barrierAction) public int await() public int getParties() public int getNumberWaiting() …….. } • Other threads • class BarrieredRuunable impl Runnable{ private CyclicBarrier barrier = …; private int result; public void run(){ … result = …; barrier.await(); } public int getResult{ return result; }} • Class BarrierAction impl Runnable{ private int finalResult = 0; public void run(){ for(Runnable r: list) finalResult += r.getResult(); } } • Rendezvous thread • BarrierdRunnable runnable;for(int i; i<N; i++){ runnable = new BarrierdRunnable(); list.add( runnable ); new Thread( runnable ).start();}CyclicBarrier barrier = new CyclicBarrier(N, new BarrierAction());