Created
October 30, 2023 01:20
-
-
Save primaryobjects/a83706847d0b4528f2971252ba77c8e9 to your computer and use it in GitHub Desktop.
Print in order using locks and semaphore, multithreaded, threads in C# https://leetcode.com/problems/print-in-order
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Threading; | |
| public class Foo { | |
| private static Object _lock = new Object(); | |
| private static int _sequence = 0; | |
| private int _limit = 100; | |
| public Foo() { | |
| } | |
| public void First(Action printFirst) { | |
| lock (_lock) { | |
| printFirst(); | |
| _sequence = 1; | |
| } | |
| } | |
| public void Second(Action printSecond) { | |
| for (int i=0; i<_limit; i++) | |
| { | |
| lock (_lock) { | |
| if (_sequence == 1) | |
| break; | |
| } | |
| Thread.Sleep(1); | |
| } | |
| lock (_lock) { | |
| printSecond(); | |
| _sequence = 2; | |
| } | |
| } | |
| public void Third(Action printThird) { | |
| for (int i=0; i<_limit; i++) | |
| { | |
| lock (_lock) { | |
| if (_sequence == 2) | |
| break; | |
| } | |
| Thread.Sleep(1); | |
| } | |
| lock (_lock) { | |
| printThird(); | |
| _sequence = 3; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment