Exercise: Concurrent Counter
This exercise requires you to implement a thread-safe counter. The program starts 10 threads, each incrementing the counter 100 times, so the final result should be 1000.
The code already provides the Mutex declaration, but the code inside the synchronized block has been removed. You need to:
- Implement the increment operation on
counterinside thesynchronizedblock - Ensure all threads synchronize correctly to avoid data races
Note
Without the mutex, due to data races, the final result may be less than 1000. Try removing
synchronized and run multiple times to observe the difference.Run the program and confirm the output is always "Final count: 1000". Then try different implementation approaches.