1 / 7

Semaphore

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.”

pearl
Download Presentation

Semaphore

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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) … }

  2. 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).

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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());

More Related