Thread Synchronization including Mutual Exclusion In Java synchronized keyword Monitor or Lock with conditions Semaphore
Semaphores Semaphore S = 3; P(S) { wait until S>0; S=S-1; } V(S) { S=S+1; }
Semaphores for Mutual Exclusion Thread 1 P(S) Access Shared Data V(S) Thread 2 P(S) Access Shared Data V(S) Semaphore S = 1; Shared Data
Semaphores for Synchronization Thread 1 P(S) Print “AAAAA” Thread 2 Print “BBBBB” V(S) Semaphore S = 0;
Semaphores for Synchronization Thread 1 P(S) Print “AAAAA” Thread 2 Print “BBBBB” V(S) Semaphore S = 0;
Semaphores in Java Semaphore s = new Semaphore(initCount); s.acquire(); or s.acquire(count); s.release(); or s.release(count);
Examples