https://github.com/mateusjssilva/semaphore-java
Java simulation of multiple nodes accessing a shared resource, demonstrating semaphore-based synchronization with correct and incorrect usage scenarios to illustrate concurrency control and deadlock.
https://github.com/mateusjssilva/semaphore-java
concurrent-programming java semaphore synchronization
Last synced: 9 months ago
JSON representation
Java simulation of multiple nodes accessing a shared resource, demonstrating semaphore-based synchronization with correct and incorrect usage scenarios to illustrate concurrency control and deadlock.
- Host: GitHub
- URL: https://github.com/mateusjssilva/semaphore-java
- Owner: MateusjsSilva
- License: mit
- Created: 2023-08-30T22:46:39.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-07T12:11:30.000Z (over 1 year ago)
- Last Synced: 2025-02-05T09:54:02.346Z (11 months ago)
- Topics: concurrent-programming, java, semaphore, synchronization
- Language: Java
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simulation of Node Behavior with Semaphores
This Java project simulates multiple nodes accessing a shared resource, using various synchronization techniques. The project demonstrates how threads behave in concurrent scenarios and how semaphores can be used correctly or incorrectly to control access to a shared resource.
## Description
The simulation includes three scenarios:
1. **Multiple simultaneous nodes (full concurrency)**: All nodes access the shared resource concurrently without any control mechanism.
2. **Correct use of the semaphore**: Nodes use a binary semaphore to ensure only one node accesses the shared resource at a time, and the semaphore is properly released.
3. **Incorrect use of the semaphore**: Nodes acquire the semaphore but do not release it, causing incorrect behavior in the system.
## Scenarios
1. **Multiple Simultaneous Nodes (Full Concurrency)**
In this scenario, all nodes try to access the shared resource concurrently, leading to possible race conditions.
2. **Correct Use of Semaphore**
Nodes are synchronized using a binary semaphore. Only one node can access the shared resource at a time, and the semaphore is properly released after use.
3. **Incorrect Use of Semaphore**
Nodes acquire the semaphore but fail to release it after accessing the shared resource, simulating a deadlock or resource lock issue.
## Installation
1. Clone the repository or download the source code.
2. Compile the Java files:
```bash
javac Simulation.java
```
3. Run the program:
```bash
java Simulation
```
## Usage
When running the program, you'll be presented with a menu to choose between the available scenarios:
- Scenario 1 - Multiple simultaneous nodes (full concurrency).
- Scenario 2 - Correct use of semaphore.
- Scenario 3 - Incorrect use of semaphore.
- Exit - Exits the simulation.
You can select a scenario by entering the corresponding number.
## Contribution
Feel free to open issues or submit pull requests. All contributions are welcome!
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.