{"id":29958525,"url":"https://github.com/kahlery/deadlock-solver","last_synced_at":"2025-08-03T20:46:47.880Z","repository":{"id":57834405,"uuid":"528342395","full_name":"kahlery/deadlock-solver","owner":"kahlery","description":"example code \u0026 implementation: Dead Lock (Reader-Writer Problem [scync] ) Solver With Mutex And Semaphores While Threads Are Working Like OS Does","archived":false,"fork":false,"pushed_at":"2023-04-18T21:19:55.000Z","size":3,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-19T14:54:20.744Z","etag":null,"topics":["deadlock","mutex","reader-writer","semaphore","synchronization","threading"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kahlery.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-08-24T08:56:21.000Z","updated_at":"2024-11-03T11:22:56.000Z","dependencies_parsed_at":"2025-05-19T14:54:28.900Z","dependency_job_id":"5231058d-31e4-4064-975b-e538388e270c","html_url":"https://github.com/kahlery/deadlock-solver","commit_stats":null,"previous_names":["kahleryasla/deadlock-solver","kahlery/deadlock-solver"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/kahlery/deadlock-solver","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kahlery%2Fdeadlock-solver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kahlery%2Fdeadlock-solver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kahlery%2Fdeadlock-solver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kahlery%2Fdeadlock-solver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kahlery","download_url":"https://codeload.github.com/kahlery/deadlock-solver/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kahlery%2Fdeadlock-solver/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268610652,"owners_count":24278150,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-03T02:00:12.545Z","response_time":2577,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deadlock","mutex","reader-writer","semaphore","synchronization","threading"],"created_at":"2025-08-03T20:46:31.596Z","updated_at":"2025-08-03T20:46:47.827Z","avatar_url":"https://github.com/kahlery.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Code Report\r\n\r\n## Introduction\r\n\r\nThe given code implements a simple example of synchronization in Java using semaphores. The main goal of this code is to demonstrate the concept of read-write locks, where multiple readers can access the shared resource simultaneously, but only one writer can access the resource at a time.\r\n\r\n## Code Walkthrough\r\n\r\n### The `sync` Class\r\n\r\nThis class serves as the entry point for the program. It creates an instance of the `ExecutorService` class and a `ReadWriteLock` object. After that, it creates four instances of the `Writer` class and four instances of the `Reader` class, passing them the `ReadWriteLock` object as a parameter. Finally, it submits all the tasks to the executor service, which runs them concurrently.\r\n\r\n### The `ReadWriteLock` Class\r\n\r\nThis class implements the read-write lock mechanism. It has three methods for acquiring and releasing the lock for reading and writing. It uses two semaphores: `mutex` and `S`. `mutex` ensures that only one thread can access the `readerCount` variable at a time, and `S` ensures that only one writer can access the shared resource at a time.\r\n\r\nThe `readLock` method acquires the `mutex` semaphore and increments the `readerCount` variable. If it is the first reader, it acquires the `S` semaphore, allowing other readers to read but blocking any writers from accessing the resource. It then releases the `mutex` semaphore.\r\n\r\nThe `writeLock` method acquires the `S` semaphore, blocking any readers or writers from accessing the resource. It then prints a message indicating that the writer is writing.\r\n\r\nThe `readUnLock` method acquires the `mutex` semaphore and decrements the `readerCount` variable. If it is the last reader, it releases the `S` semaphore, allowing writers to access the resource. It then releases the `mutex` semaphore.\r\n\r\nThe `writeUnLock` method prints a message indicating that the writer is done writing and releases the `S` semaphore, allowing other writers to access the resource.\r\n\r\n### The `SleepUtilities` Class\r\n\r\nThis class contains a utility method `nap` that causes the current thread to sleep for a random amount of time between 0 and 5 seconds.\r\n\r\n### The `Writer` Class\r\n\r\nThis class implements the `Runnable` interface and represents a writer thread. It has a `ReadWriteLock` object and a `writerNum` variable as instance variables.\r\n\r\nThe `run` method is an infinite loop that sleeps for a random amount of time, attempts to acquire the write lock, sleeps again, and releases the write lock.\r\n\r\n### The `Reader` Class\r\n\r\nThis class implements the `Runnable` interface and represents a reader thread. It has a `ReadWriteLock` object and a `readerNum` variable as instance variables.\r\n\r\nThe `run` method is an infinite loop that sleeps for a random amount of time, attempts to acquire the read lock, sleeps again, and releases the read lock.\r\n\r\n## Conclusion\r\n\r\nIn conclusion, this code implements a simple example of synchronization in Java using semaphores. It demonstrates the concept of read-write locks and how multiple readers can access the shared resource simultaneously, but only one writer can access the resource at a time.\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkahlery%2Fdeadlock-solver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkahlery%2Fdeadlock-solver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkahlery%2Fdeadlock-solver/lists"}