F
Frank Gerlach
I hope I got it right this time. Please comment.
/** Semaphore class for managing limited resources (such as database
* connections)
* Author: Frank Gerlach ([email protected])
* Copyright: None. Use in any way you want.
*/
public class Semaphore{
int count;
/** Create the semaphore
* @param initialcount number of resource items to manage
*/
public Semaphore(int initialcount){
count=initialcount;
}
/** Acquire one resource item.
*/
public synchronized void P() throws InterruptedException{
while(count==0){
wait();
}
if(count<0)throw new Error("Fatal problem in Semaphore");//just in case
count--;
}
/** Release one resource item.
*/
public synchronized void V(){
count++;
notify();//always notify(), necessary if multiple threads sleep in P()
}
}
/** Semaphore class for managing limited resources (such as database
* connections)
* Author: Frank Gerlach ([email protected])
* Copyright: None. Use in any way you want.
*/
public class Semaphore{
int count;
/** Create the semaphore
* @param initialcount number of resource items to manage
*/
public Semaphore(int initialcount){
count=initialcount;
}
/** Acquire one resource item.
*/
public synchronized void P() throws InterruptedException{
while(count==0){
wait();
}
if(count<0)throw new Error("Fatal problem in Semaphore");//just in case
count--;
}
/** Release one resource item.
*/
public synchronized void V(){
count++;
notify();//always notify(), necessary if multiple threads sleep in P()
}
}