}
public synchronized void readUnlock(){
readCnt--;
if(readCnt==0){
state=EMPTY;
notify();
}
}
public synchronized void writeUnlock(){
state=EMPTY;
notify();
}
}
现在是测试信号标志的程序:
class Process extends Thread{
String op;
Semaphore sem;
Process(String name,String op,Semaphore sem){
super(name);
this.op=op;
this.sem=sem;
start();
}
public void run(){
if(op.compareTo("read")==0){
System.out.println("Trying to get readLock:"+getName());
sem.readLock();
System.out.println("Read op:"+getName());
try {sleep((int)(Math.random()*50));}
catch(InterruptedException e){;}
System.out.println("Unlocking readLock:"+getName());
sem.readUnlock();
}
else if(op.compareTo("write")==0){
System.out.println("Trying to get writeLock:"+getName());

