common-util

local.tomas.util.sync
Class Semaphore

java.lang.Object
  extended by local.tomas.util.sync.Semaphore
All Implemented Interfaces:
SyncInterface

public class Semaphore
extends Object
implements SyncInterface

A Semaphore is a kind of synchronized counter. It can be used to limit access to a resource to a specific number of clients.

Version:
1.0
Author:
Tomas Teubner

Constructor Summary
Semaphore(int count)
          Construct semaphore.
 
Method Summary
 void lock()
          Lock 1 resource.
 void lock(int n)
          Lock n resources.
 boolean trylock()
          Try to lock 1 resource.
 boolean trylock(int n)
          Try to lock n resources.
 void unlock()
          Unlock 1 resource.
 void unlock(int n)
          Unlock n resources
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Semaphore

public Semaphore(int count)
Construct semaphore.

Parameters:
count - number of resources controlled by this semaphore
Method Detail

trylock

public boolean trylock(int n)
Try to lock n resources. If the number of available resources is less than n, false is returned.

Parameters:
n - number of resources
Returns:
true if lock succeeded, false otherwise

trylock

public boolean trylock()
Try to lock 1 resource.

Specified by:
trylock in interface SyncInterface
Returns:
true if lock succeeded, false otherwise

lock

public void lock(int n)
          throws InterruptedException
Lock n resources. If the number of available resources is less than n, the call to this method will block until they become available.

Parameters:
n - number of resources
Throws:
InterruptedException

lock

public void lock()
          throws InterruptedException
Lock 1 resource.

Specified by:
lock in interface SyncInterface
Throws:
InterruptedException

unlock

public void unlock(int n)
Unlock n resources

Parameters:
n - number of resources

unlock

public void unlock()
Unlock 1 resource.

Specified by:
unlock in interface SyncInterface

common-util

Copyright © 2005-2007 Tomas Teubner. All Rights Reserved.