package Paradigms.List11.CommonStaticVariableResolvings;
import java.util.concurrent.Semaphore;
class CountSemaphore extends Thread{
static IntCellDefault n = new IntCellDefault();
private static Semaphore mutex = new Semaphore(1, true);
// first arg permit = liczba pozwolen, definiuje liczbe watkow ktora w jednym czasie moze uzyskac dostep do
// sekcji krytycznej, maleje ona w dol
// second arg fair = uczciowosc, polegajaca na szeregowaniu watkow oczekujacych na dostep do sekcji krytycznej
// przy pomocy kolejki FIFO
// nazwa zmiennej mutex poniewaz semaphore z umozliwiajacy tylko jedno pozwolenie jest semaforem binarnym czyli
// tzw. mutexem
@Override
public void run(){
for (int i = 0; i < 200000; i++){
tryAcquire();
int temp = n.getN();
n.setN(temp+1);
mutex.release();
}
}
private void tryAcquire(){
try{
mutex.acquire();
}catch(InterruptedException e){
e.printStackTrace();
}
}
public static void main(String[] args){
CountSemaphore thread1 = new CountSemaphore();
CountSemaphore thread2 = new CountSemaphore();
thread1.start();
thread2.start();
try{
thread1.join();
thread2.join();
}catch(InterruptedException e){
System.out.println(e.getStackTrace());
System.out.println("Catched exception");
}
System.out.println("The value of n is " + n.getN());
System.out.println("Used semaphore");
}
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}