"CountingSemaphoreTest" defines the main method for this program. This example illustrates how couting semaphores can be used to implement mutual exclusion. The class "CountingSemaphore" defines a standard counting semaphore holding an integer value and supporting the usual "P" and "V" operations. A "P" operation decrements the semaphore's value and blocks if the new value is less than zero. A "V" operation increments the semaphore's value and releases one blocked thread if the new value is zero or less. The definition of the two anonymous inner classes shows how "P" and "V" can be used to build a mutual exclusion locks. Points to note are (i) the semaphore is initialised with the value 1, meaning that at most one "P" operation can succeed until "V" is invoked, (ii) the critical region is bounded by a call to "P" at the start and a call to "V" at the end. You could think of these as decrementing and incrementing a count of the number of threads that are allowed into the critical region. It's often easier to remember how to implement a mutex using counting semaphores and to use that to derive the behaviour of "P" and "V" rather than remembering the details of those two operations. This program "CountingSemaphoreTest" uses two threads which add and substract to the shared fields "a" and "b". It runs each thread for 100000 iterations of the loop and then checks that the total of "a" and "b" remains that same as when the threads started.