The Synchronization Simulator Project is designed to demonstrate and compare the effects of synchronization on shared data accessed by multiple concurrent threads. This project explores how synchronization mechanisms can ensure data consistency in a multithreaded environment.
- Simulate a multithreaded system where producer and consumer threads modify a shared variable.
- Compare the results with and without synchronization.
- Highlight the importance of synchronization tools like semaphores in managing access to critical sections.
-
Programming Language:
Java -
Core Classes:
Data: Manages the shared variable.Producer: Implements producer threads to increment the shared variable.Consumer: Implements consumer threads to decrement the shared variable.OS: Simulates the system without synchronization.OSWithSync: Simulates the system with synchronization using semaphores.
-
Thread Management:
- Randomized number of threads =>
producersandconsumers.
- Controlled operations ensuring adherence to conditions:
- Shared variable
Xstarts at1000. - Producers increment
Xbut stop if it exceeds1800. - Consumers decrement
Xbut stop if it drops below0.
- Shared variable
- Randomized number of threads =>
- Without Synchronization:
- Producer and consumer threads access the shared variable concurrently without any synchronization.
- Results vary depending on the order of thread execution.
- With Synchronization:
- Semaphore-based synchronization ensures that only one thread accesses the shared variable at a time.
- Results are consistent and adhere to the defined conditions.
- Execution Flow:
- Randomized thread generation with random numbers of operations.
- Results are displayed for both synchronized and non-synchronized executions.
-
Data:- Defines the shared variable
X. - Initializes
Xto 1000.
- Defines the shared variable
-
Producer:- Implements threads that increment the shared variable.
- Ensures
Xdoes not exceed 1800.
-
Consumer:- Implements threads that decrement the shared variable.
- Ensures
Xdoes not fall below 0.
-
OS:- Runs producer and consumer threads without synchronization.
- Displays results showcasing inconsistencies caused by thread interference.
-
OSWithSync:- Uses semaphores (
acquireandreleasemethods) to synchronize thread access. - Ensures consistent results regardless of thread execution order.
- Uses semaphores (
Note
for more detailed info you can read the project Report
- Results depend heavily on the order of thread execution.
- Shared variable X often deviates from expected values due to race conditions.
- Results are consistent and align with the conditions set for the shared variable.
- Demonstrates the effectiveness of semaphore-based synchronization.
- Understand the impact of synchronization in multithreaded systems.
- Analyze the behavior of shared data with and without synchronization tools.
- Gain hands-on experience with Java threads and semaphore mechanisms.
