Date and Time: Feb 17, 2025, 16:21 (EST)
Link: https://leetcode.com/problems/print-in-order
Suppose we have a class:
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.
numsis a permutation of[1, 2, 3].
Use semophores to hold the resources, and second() needs to wait for firstJobDone to finish to unlock. third() needs to wait for secondJobDone to unlock.
#include <semaphore.h>
class Foo {
protected:
// Create two semophores to lock resources
sem_t firstJobDone;
sem_t secondJobDone;
public:
Foo() {
// Initialize here
sem_init(&firstJobDone, 0, 0);
sem_init(&secondJobDone, 0, 0);
}
void first(function<void()> printFirst) {
// printFirst() outputs "first". Do not change or remove this line.
printFirst();
sem_post(&firstJobDone); // Release firstJobDone
}
void second(function<void()> printSecond) {
sem_wait(&firstJobDone); // Wait for firstJobDone to release resource
// printSecond() outputs "second". Do not change or remove this line.
printSecond();
sem_post(&secondJobDone); // Release secondJobDone
}
void third(function<void()> printThird) {
sem_wait(&secondJobDone); // Wait for secondJobDone
// printThird() outputs "third". Do not change or remove this line.
printThird();
}
};class Foo {
private AtomicInteger firstJobDone = new AtomicInteger(0);
private AtomicInteger secondJobDone = new AtomicInteger(0);
public Foo() {}
public void first(Runnable printFirst) throws InterruptedException {
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
firstJobDone.incrementAndGet(); // Increment count to mark as Done
}
public void second(Runnable printSecond) throws InterruptedException {
// Make second() waiting before we release firstJobDone
while (firstJobDone.get() != 1) {}
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
secondJobDone.incrementAndGet();
}
public void third(Runnable printThird) throws InterruptedException {
while (secondJobDone.get() != 1) {}
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}
}from threading import Lock
class Foo:
def __init__(self):
self.firstJobDone = Lock()
self.secondJobDone = Lock()
self.firstJobDone.acquire()
self.secondJobDone.acquire()
def first(self, printFirst: 'Callable[[], None]') -> None:
# printFirst() outputs "first".
printFirst()
# Notify the thread that is waiting for the first job to be done.
self.firstJobDone.release()
def second(self, printSecond: 'Callable[[], None]') -> None:
# Wait for the first job to be done
with self.firstJobDone:
# printSecond() outputs "second".
printSecond()
# Notify the thread that is waiting for the second job to be done.
self.secondJobDone.release()
def third(self, printThird: 'Callable[[], None]') -> None:
# Wait for the second job to be done.
with self.secondJobDone:
# printThird() outputs "third".
printThird()