Semaphore - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Semaphore

Description:

Similar to lock as it can be used to prevent access to shared data when the data ... grabs a permit and decrements the semaphore value (# of permits) release ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 8
Provided by: Informatio358
Category:
Tags: grabs | semaphore

less

Transcript and Presenter's Notes

Title: Semaphore


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.concurrentpublic 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()
  • Whats 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 runnablefor(int i iltN i)
    runnable new BarrierdRunnable()
    list.add( runnable ) new Thread( runnable
    ).start()CyclicBarrier barrier new
    CyclicBarrier(N, new BarrierAction())
Write a Comment
User Comments (0)
About PowerShow.com