דגעדגע
Semaphore mutex = new Semaphore(1);
int readers = 0; //Initial number of threads inside the critical section
public int getRadius(){
mutex.acqire(); //Approaching common resources
readers++;
mutex.release();
//Now other readers may read the radius
//**************************************************
int tempRadius = radius;
//**************************************************
mutex.acquire(); //Adain, approaching common resources
readers--;
mutex.release();
return tempRadius;
}
public void setRadius(int newRadius){
mutex.acqire(); //Approaching common resources
while(readers > 0){
mutex.release();
//Let someone else acquire the mutex and set count
mutex.acquire();
}
radius = newRadius;
mutex.release;
}