Print in Order
<p>Suppose we have a class:</p>

<pre>
public class Foo {
  public void first() { print(&quot;first&quot;); }
  public void second() { print(&quot;second&quot;); }
  public void third() { print(&quot;third&quot;); }
}
</pre>

<p>The same instance of <code>Foo</code> will be passed to three different threads. Thread A will call <code>first()</code>, thread B will call <code>second()</code>, and thread C will call <code>third()</code>. Design a mechanism and modify the program to ensure that <code>second()</code> is executed after <code>first()</code>, and <code>third()</code> is executed after <code>second()</code>.</p>

<p><strong>Note:</strong></p>

<p>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&#39; comprehensiveness.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> &quot;firstsecondthird&quot;
<strong>Explanation:</strong> 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(). &quot;firstsecondthird&quot; is the correct output.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,3,2]
<strong>Output:</strong> &quot;firstsecondthird&quot;
<strong>Explanation:</strong> The input [1,3,2] means thread A calls first(), thread B calls third(), and thread C calls second(). &quot;firstsecondthird&quot; is the correct output.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>nums</code> is a permutation of <code>[1, 2, 3]</code>.</li>
</ul>

['print-foobar-alternately']
[]
[{"value": "cpp", "text": "C++", "defaultCode": "class Foo {\npublic:\n    Foo() {\n        \n    }\n\n    void first(function<void()> printFirst) {\n        \n        // printFirst() outputs \"first\". Do not change or remove this line.\n        printFirst();\n    }\n\n    void second(function<void()> printSecond) {\n        \n        // printSecond() outputs \"second\". Do not change or remove this line.\n        printSecond();\n    }\n\n    void third(function<void()> printThird) {\n        \n        // printThird() outputs \"third\". Do not change or remove this line.\n        printThird();\n    }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Foo {\n\n    public Foo() {\n        \n    }\n\n    public void first(Runnable printFirst) throws InterruptedException {\n        \n        // printFirst.run() outputs \"first\". Do not change or remove this line.\n        printFirst.run();\n    }\n\n    public void second(Runnable printSecond) throws InterruptedException {\n        \n        // printSecond.run() outputs \"second\". Do not change or remove this line.\n        printSecond.run();\n    }\n\n    public void third(Runnable printThird) throws InterruptedException {\n        \n        // printThird.run() outputs \"third\". Do not change or remove this line.\n        printThird.run();\n    }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Foo(object):\n    def __init__(self):\n        pass\n\n\n    def first(self, printFirst):\n        \"\"\"\n        :type printFirst: method\n        :rtype: void\n        \"\"\"\n        \n        # printFirst() outputs \"first\". Do not change or remove this line.\n        printFirst()\n\n\n    def second(self, printSecond):\n        \"\"\"\n        :type printSecond: method\n        :rtype: void\n        \"\"\"\n        \n        # printSecond() outputs \"second\". Do not change or remove this line.\n        printSecond()\n            \n            \n    def third(self, printThird):\n        \"\"\"\n        :type printThird: method\n        :rtype: void\n        \"\"\"\n        \n        # printThird() outputs \"third\". Do not change or remove this line.\n        printThird()"}, {"value": "python3", "text": "Python3", "defaultCode": "class Foo:\n    def __init__(self):\n        pass\n\n\n    def first(self, printFirst: 'Callable[[], None]') -> None:\n        \n        # printFirst() outputs \"first\". Do not change or remove this line.\n        printFirst()\n\n\n    def second(self, printSecond: 'Callable[[], None]') -> None:\n        \n        # printSecond() outputs \"second\". Do not change or remove this line.\n        printSecond()\n\n\n    def third(self, printThird: 'Callable[[], None]') -> None:\n        \n        # printThird() outputs \"third\". Do not change or remove this line.\n        printThird()"}, {"value": "c", "text": "C", "defaultCode": "typedef struct {\n    // User defined data may be declared here.\n    \n} Foo;\n\n// Function Declaration, do not remove\nvoid printFirst();\nvoid printSecond();\nvoid printThird();\n\nFoo* fooCreate() {\n    Foo* obj = (Foo*) malloc(sizeof(Foo));\n    \n    // Initialize user defined data here.\n    \n    return obj;\n}\n\nvoid first(Foo* obj) {\n    \n    // printFirst() outputs \"first\". Do not change or remove this line.\n    printFirst();\n}\n\nvoid second(Foo* obj) {\n    \n    // printSecond() outputs \"second\". Do not change or remove this line.\n    printSecond();\n}\n\nvoid third(Foo* obj) {\n    \n    // printThird() outputs \"third\". Do not change or remove this line.\n    printThird();\n}\n\nvoid fooFree(Foo* obj) {\n    // User defined data may be cleaned up here.\n    \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Foo {\n\n    public Foo() {\n        \n    }\n\n    public void First(Action printFirst) {\n        \n        // printFirst() outputs \"first\". Do not change or remove this line.\n        printFirst();\n    }\n\n    public void Second(Action printSecond) {\n        \n        // printSecond() outputs \"second\". Do not change or remove this line.\n        printSecond();\n    }\n\n    public void Third(Action printThird) {\n        \n        // printThird() outputs \"third\". Do not change or remove this line.\n        printThird();\n    }\n}"}]
{
  "name": "foobar",
  "params": [
    {
      "name": "nums",
      "type": "integer[]"
    }
  ],
  "return": {
    "type": "integer"
  },
  "languages": [
    "java",
    "cpp",
    "python",
    "python3",
    "c",
    "csharp"
  ],
  "manual": true
}
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n    int id;            /* we'll use this field as the key */\r\n    char name[10];\r\n    UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n    HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n    struct hash_entry *s;\r\n    HASH_FIND_INT(users, &user_id, s);\r\n    return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n    HASH_DEL(users, user);  \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"]}
['Concurrency']