Algorithm
Problem Name: 1114. 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.
Constraints:
nums
is a permutation of[1, 2, 3]
.
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
class Foo {
public:
int count = 0;
mutex mtx;
condition_variable cv;
Foo() {
count = 1;
}
void first(function < void()> printFirst) {
unique_lock lck(mtx);
// printFirst() outputs "first". Do not change or remove this line.
printFirst();
count = 2;
cv.notify_all();
}
void second(function < void()> printSecond) {
unique_lock lck(mtx);
cv.wait(lck, [this]() { return count == 2;});
// printSecond() outputs "second". Do not change or remove this line.
printSecond();
count = 3;
cv.notify_all();
}
void third(function < void()> printThird) {
unique_lock lck(mtx);
cv.wait(lck, [this]() { return count == 3;});
// printThird() outputs "third". Do not change or remove this line.
printThird();
}
};
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Python Programming
Code -
Python Programming
import threading
class Foo(object):
def __init__(self):
self.two = threading.Semaphore()
self.three = threading.Semaphore()
self.two.acquire()
self.three.acquire()
def first(self, printFirst):
"""
:type printFirst: method
:rtype: void
"""
# printFirst() outputs "first". Do not change or remove this line.
printFirst()
self.two.release()
def second(self, printSecond):
"""
:type printSecond: method
:rtype: void
"""
self.two.acquire()
# printSecond() outputs "second". Do not change or remove this line.
printSecond()
self.three.release()
def third(self, printThird):
"""
:type printThird: method
:rtype: void
"""
self.three.acquire()
# printThird() outputs "third". Do not change or remove this line.
printThird()
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with C# Programming
Code -
C# Programming
using System;
using System.Threading;
namespace LeetCode
{
public class Foo
{
private EventWaitHandle waitHandle1;
private EventWaitHandle waitHandle2;
public Foo()
{
waitHandle1 = new AutoResetEvent(false);
waitHandle2 = new AutoResetEvent(false);
}
public void First(Action printFirst)
{
// printFirst() outputs "first". Do not change or remove this line.
printFirst();
waitHandle1.Set();
}
public void Second(Action printSecond)
{
waitHandle1.WaitOne();
// printSecond() outputs "second". Do not change or remove this line.
printSecond();
waitHandle2.Set();
}
public void Third(Action printThird)
{
waitHandle2.WaitOne();
// printThird() outputs "third". Do not change or remove this line.
printThird();
}
}
}
Copy The Code &
Try With Live Editor
Input
Output