semaphore l.
Skip this Video
Loading SlideShow in 5 Seconds..
Semaphore PowerPoint Presentation
Download Presentation

Loading in 2 Seconds...

play fullscreen
1 / 7

Semaphore - PowerPoint PPT Presentation

  • Uploaded on

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
  • 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
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
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.
  • 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
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.
  • 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());