comments | difficulty | edit_url | tags | |
---|---|---|---|---|
true |
Easy |
|
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.
Constraints:
nums
is a permutation of[1, 2, 3]
.
We can use three semaphores
When thread first()
method, it first needs to acquire semaphore first()
method, and then releases semaphore second()
method.
When thread second()
method, it first needs to acquire semaphore second()
method, and then releases semaphore third()
method.
When thread third()
method, it first needs to acquire semaphore third()
method, and then releases semaphore first()
method.
The time complexity is
class Foo:
def __init__(self):
self.l2 = threading.Lock()
self.l3 = threading.Lock()
self.l2.acquire()
self.l3.acquire()
def first(self, printFirst: 'Callable[[], None]') -> None:
printFirst()
self.l2.release()
def second(self, printSecond: 'Callable[[], None]') -> None:
self.l2.acquire()
printSecond()
self.l3.release()
def third(self, printThird: 'Callable[[], None]') -> None:
self.l3.acquire()
printThird()
class Foo {
private Semaphore a = new Semaphore(1);
private Semaphore b = new Semaphore(0);
private Semaphore c = new Semaphore(0);
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
a.acquire(1);
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
b.release(1);
}
public void second(Runnable printSecond) throws InterruptedException {
b.acquire(1);
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
c.release(1);
}
public void third(Runnable printThird) throws InterruptedException {
c.acquire(1);
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
a.release(1);
}
}
class Foo {
private:
mutex m2, m3;
public:
Foo() {
m2.lock();
m3.lock();
}
void first(function<void()> printFirst) {
printFirst();
m2.unlock();
}
void second(function<void()> printSecond) {
m2.lock();
printSecond();
m3.unlock();
}
void third(function<void()> printThird) {
m3.lock();
printThird();
}
};
from threading import Semaphore
class Foo:
def __init__(self):
self.a = Semaphore(1)
self.b = Semaphore(0)
self.c = Semaphore(0)
def first(self, printFirst: 'Callable[[], None]') -> None:
self.a.acquire()
# printFirst() outputs "first". Do not change or remove this line.
printFirst()
self.b.release()
def second(self, printSecond: 'Callable[[], None]') -> None:
self.b.acquire()
# printSecond() outputs "second". Do not change or remove this line.
printSecond()
self.c.release()
def third(self, printThird: 'Callable[[], None]') -> None:
self.c.acquire()
# printThird() outputs "third". Do not change or remove this line.
printThird()
self.a.release()
#include <semaphore.h>
class Foo {
private:
sem_t a, b, c;
public:
Foo() {
sem_init(&a, 0, 1);
sem_init(&b, 0, 0);
sem_init(&c, 0, 0);
}
void first(function<void()> printFirst) {
sem_wait(&a);
// printFirst() outputs "first". Do not change or remove this line.
printFirst();
sem_post(&b);
}
void second(function<void()> printSecond) {
sem_wait(&b);
// printSecond() outputs "second". Do not change or remove this line.
printSecond();
sem_post(&c);
}
void third(function<void()> printThird) {
sem_wait(&c);
// printThird() outputs "third". Do not change or remove this line.
printThird();
sem_post(&a);
}
};