Class Foo

java.lang.Object
g1101_1200.s1114_print_in_order.Foo

public class Foo extends Object
1114 - Print in Order.<p>Easy</p> <p>Suppose we have a class:</p> <p>public class Foo { public void first() { print(&ldquo;first&rdquo;); } public void second() { print(&ldquo;second&rdquo;); } public void third() { print(&ldquo;third&rdquo;); } }</p> <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&rsquo; comprehensiveness.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [1,2,3]</p> <p><strong>Output:</strong> &ldquo;firstsecondthird&rdquo;</p> <p><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(). &ldquo;firstsecondthird&rdquo; is the correct output.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,3,2]</p> <p><strong>Output:</strong> &ldquo;firstsecondthird&rdquo;</p> <p><strong>Explanation:</strong> The input [1,3,2] means thread A calls first(), thread B calls third(), and thread C calls second(). &ldquo;firstsecondthird&rdquo; is the correct output.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums</code> is a permutation of <code>[1, 2, 3]</code>.</li> </ul>
  • Constructor Details

    • Foo

      public Foo()
  • Method Details

    • first

      public void first(Runnable printFirst)
    • second

      public void second(Runnable printSecond)
    • third

      public void third(Runnable printThird)