A large ornamental garden is open to members of the public who can enter through either one of two turnstiles to the East and to the West of the garden. The management wants to determine how many people are in the garden at any one time. They design a computer system to do this. In the program produced, each turnstile is represented by a thread and updates a shared Counter object.
This version does not work correctly as can be observed (click on the "Go" button and 40 people will enter the garden, 20 from each side); increments to the Counter are lost so that the total number of people in the garden is not the sum of the people who entered through the turnstiles.
This applet illustrates the so-called "Write/Write conflict". We have concurrently executing update methods which result in an inconsistent assignment to variables of the shared counter object.
Define a new Thread-safe, synchronized counter class as a subclass of Counter and modify the applet Garden appropriately.
Garden.java, Counter.java, Simulate.java, Turnstile.java, NumberPanel.java
Using a synchronized counter class the program works correctly.