Generalnie chodzi o to, żeby program wyświetlał BCBA w kółko i wyświetla tylko że na końcu powinno się wyświetlić dla każdej literki Thread B: I'm done... a wyświetla się tak tylko dla litery B czyli procesy nie są pokończone Tutaj wklejam fragment kodu z synchronizacją
private static final class A extends Thread { //thread definition
@Override
@SuppressWarnings("SleepWhileInLoop")
public void run() {
try {
for (int i = 0; i < COUNT; i++) {
//use semaphores here
a.acquire();
a.acquire();
System.out.print("A ");
//use semaphores here
b.release();
Thread.sleep(DELAY);
}
} catch (InterruptedException ex) {
System.out.println("Ooops...");
Thread.currentThread().interrupt();
throw new RuntimeException(ex);
}
System.out.println("\nThread A: I'm done...");
}
}
private static final class B extends Thread {
@Override
@SuppressWarnings("SleepWhileInLoop")
public void run() {
try {
for (int i = 0; i < COUNT; i++) {
//use semaphores here
b.acquire();
System.out.print("B ");
//use semaphores here
a.release();
c.release();
Thread.sleep(DELAY);
}
} catch (InterruptedException ex) {
System.out.println("Ooops...");
Thread.currentThread().interrupt();
throw new RuntimeException(ex);
}
System.out.println("\nThread B: I'm done...");
}
}
private static final class C extends Thread {
@Override
@SuppressWarnings("SleepWhileInLoop")
public void run() {
try {
for (int i = 0; i < COUNT; i++) {
//use semaphores here
c.acquire();
c.acquire();
System.out.print("C ");
//use semaphores here
b.release();
Thread.sleep(DELAY);
}
} catch (InterruptedException ex) {
System.out.println("Ooops...");
Thread.currentThread().interrupt();
throw new RuntimeException(ex);
}
System.out.println("\nThread C: I'm done...");
}
}
Wartości początkowe semaforów to s(a) = 0, s(b) = 1, s(c) = 1
A po uruchomieniu program wygląda tak: