@@ -0,0 +1,1878 @@
+import java.util.concurrent.*;%0A%0Aclass SemDemo %7B%0A    public static void main(String%5B%5D args) %7B%0A        Semaphore semaphore = new Semaphore(1);%0A%0A        new BaseThread(semaphore, %22A%22, new IncrementOperation());%0A        new BaseThread(semaphore, %22B%22, new DecrementOperation());%0A    %7D%0A%7D%0A%0A%0Aclass Shared %7B%0A    static int count = 0;%0A%7D%0A%0Ainterface Strategy %7B%0A    void performOperation() throws InterruptedException;%0A%7D%0A%0Aclass IncrementOperation implements Strategy %7B%0A    // %5BStrategy%5D implementation%0A    public void performOperation() throws InterruptedException %7B%0A        for (int i = 0; i %3C 5; i++) %7B%0A            Shared.count++;%0A            System.out.println(%22: %22 + Shared.count);%0A            Thread.sleep(10);%0A        %7D%0A    %7D%0A%7D%0A%0Aclass DecrementOperation implements Strategy %7B%0A    // %5BStrategy%5D implementation%0A    public void performOperation() throws InterruptedException %7B%0A        for (int i = 5; i %3E 0; i--) %7B%0A            Shared.count--;%0A            System.out.println(%22: %22 + Shared.count);%0A            Thread.sleep(10);%0A        %7D%0A    %7D%0A%7D%0A%0Aclass BaseThread implements Runnable %7B%0A    Semaphore semaphore;%0A    String name;%0A    Strategy strategy;%0A%0A    BaseThread(Semaphore semaphore, String name, Strategy strategy) %7B%0A        this.semaphore = semaphore;%0A        this.name = name;%0A        this.strategy = strategy;%0A        new Thread(this).start();%0A    %7D%0A%0A    // %5BRunnable%5D implementation%0A    public void run() %7B%0A        System.out.println(%22Starting %22 + name);%0A%0A        try %7B%0A            System.out.println(name + %22 is waiting for a permit%22);%0A            semaphore.acquire();%0A            System.out.println(name + %22 gets a permit.%22);%0A%0A            strategy.performOperation();%0A%0A        %7D%0A        catch (InterruptedException exception) %7B%0A            System.out.println(exception);%0A        %7D%0A%0A        System.out.println(name + %22 releases the permit.%22);%0A        semaphore.release();%0A    %7D%0A%7D%0A

refactor the previuos example with semahpores