1114. Print in Order
use System.Threading.Tasks
public class Foo {
public void first() { print("first"); }
public void second() { print("second"); }
public void third() { print("third"); }
}Input: [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.Input: [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.using System.Threading.Tasks;
public class Foo {
private TaskCompletionSource<bool> task_complete1 = new TaskCompletionSource<bool>();
private TaskCompletionSource<bool> task_complete2 = new TaskCompletionSource<bool>();
public Foo() {
}
public void First(Action printFirst) {
// printFirst() outputs "first". Do not change or remove this line.
printFirst();
task_complete1.SetResult(true);
}
public void Second(Action printSecond) {
task_complete1.Task.Wait();
// printSecond() outputs "second". Do not change or remove this line.
printSecond();
task_complete2.SetResult(true);
}
public void Third(Action printThird) {
task_complete2.Task.Wait();
// printThird() outputs "third". Do not change or remove this line.
printThird();
}
}Last updated