Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/emahtab/print-in-order
https://github.com/emahtab/print-in-order
java leetcode semaphore
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/emahtab/print-in-order
- Owner: eMahtab
- Created: 2024-10-31T14:02:02.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-10-31T14:04:52.000Z (3 months ago)
- Last Synced: 2024-10-31T15:18:23.285Z (3 months ago)
- Topics: java, leetcode, semaphore
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Print in order
## https://leetcode.com/problems/print-in-orderSuppose we have a class:
```java
public class Foo {
public void first() { print("first"); }
public void second() { print("second"); }
public void third() { print("third"); }
}
```
The same instance of Foo will be passed to three different threads. Thread A will call first(), thread B will call second(), and thread C will call third(). Design a mechanism and modify the program to ensure that second() is executed after first(), and third() is executed after second().Note:
We do not know how the threads will be scheduled in the operating system, even though the numbers in the input seem to imply the ordering. The input format you see is mainly to ensure our tests' comprehensiveness.
```
Example 1:Input: nums = [1,2,3]
Output: "firstsecondthird"
Explanation: There are three threads being fired asynchronously. The input [1,2,3] means thread A calls first(),
thread B calls second(), and thread C calls third(). "firstsecondthird" is the correct output.Example 2:
Input: nums = [1,3,2]
Output: "firstsecondthird"
Explanation: The input [1,3,2] means thread A calls first(), thread B calls third(),
and thread C calls second(). "firstsecondthird" is the correct output.
```# Implementation : Using Semaphore
```java
class Foo {
Semaphore first = new Semaphore(1);
Semaphore second = new Semaphore(0);
Semaphore third = new Semaphore(0);public Foo() {
}public void first(Runnable printFirst) throws InterruptedException {
first.acquire();
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
second.release();
}public void second(Runnable printSecond) throws InterruptedException {
second.acquire();
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
third.release();
}public void third(Runnable printThird) throws InterruptedException {
third.acquire();
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
third.release();
}
}
```# References :
https://jenkov.com/tutorials/java-util-concurrent/semaphore.html