✒️
Leetcode
  • Leetcode
  • Ruby
    • 1. Two Sum
    • 7. Reverse Integer
    • 9. Palindrome Number
    • 13. Roman to Integer
    • 14. Longest Common Prefix
    • 20. Valid Parentheses
    • 21. Merge Two Sorted Lists
    • 26. Remove Duplicates from Sorted Array
    • 27. Remove Element
    • 28. Implement strStr()
    • 35. Search Insert Position
    • 38. Count and Say
    • 53. Maximum Subarray
    • 58. Length of Last Word
    • 66. Plus One
    • 67. Add Binary
    • 69. Sqrt(x)
    • 70. Climbing Stairs
    • 83. Remove Duplicates from Sorted List
    • 88. Merge Sorted Array
    • 100. Same Tree
    • 101. Symmetric Tree
    • 104. Maximum Depth of Binary Tree
    • 121. Best Time to Buy and Sell Stock
    • 122. Best Time to Buy and Sell Stock II
    • 125. Valid Palindrome
    • 136. Single Number
    • 141. Linked List Cycle
    • 155. Min Stack
    • 160. Intersection of Two Linked Lists
    • 169. Majority Element
    • 191. Number of 1 Bits
    • 198. House Robber
    • 203. Remove Linked List Elements
    • 205. Isomorphic Strings
    • 206. Reverse Linked List
    • 217. Contains Duplicate
    • 226. Invert Binary Tree
    • 231. Power of Two
    • 234. Palindrome Linked List
    • 263. Ugly Number
    • 283. Move Zeroes
    • 292. Nim Game
    • 344. Reverse String
    • 350. Intersection of Two Arrays II
    • 371. Sum of Two Integers
    • 387. First Unique Character in a String
    • 404. Sum of Left Leaves
    • 401. Binary Watch
    • 448. Find All Numbers Disappeared in an Array
    • 922. Sort Array By Parity II
    • 1051. Height Checker
    • 1380. Lucky Numbers in a Matrix
  • C#
    • 1. Two Sum
    • 7. Reverse Integer
    • 9. Palindrome Number
    • 13. Roman to Integer
    • 14. Longest Common Prefix
    • 20. Valid Parentheses
    • 21. Merge Two Sorted Lists
    • 26. Remove Duplicates from Sorted Array
    • 27. Remove Element
    • 28. Implement strStr()
    • 35. Search Insert Position
    • 38. Count and Say
    • 53. Maximum Subarray
    • 58. Length of Last Word
    • 191. Number of 1 Bits
    • 231. Power of Two
    • 401. Binary Watch
    • 1114. Print in Order
Powered by GitBook
On this page

Was this helpful?

  1. C#

1114. Print in Order

use System.Threading.Tasks

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().

Example 1:

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.

Example 2:

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.

Note:

We do not know how the threads will be scheduled in the operating system, even though the numbers in the input seems to imply the ordering. The input format you see is mainly to ensure our tests' comprehensiveness.

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();
    }
}
Previous401. Binary Watch

Last updated 4 years ago

Was this helpful?