LeetCode:1114.順次印刷

1496 ワード

タイトルリンク
クラスを提供します.
public class Foo {   public void one() { print("one"); }   public void two() { print("two"); }   public void three() { print("three"); } } 3つの異なるスレッドは、1つのFooインスタンスを共有します.
スレッドAがone()メソッドスレッドBを呼び出すtwo()メソッドスレッドCがthree()メソッドを呼び出すthree()メソッドを呼び出すtwo()メソッドの後にtwo()メソッドが実行され、three()メソッドの後にthree()メソッドが実行されるように修正プログラムを設計してください.
 
例1:
入力:[1,2,3]出力:[onetwothree]解釈:3つのスレッドが非同期で起動されます.[1,2,3]を入力すると、スレッドAがone()メソッドを呼び出し、スレッドBがtwo()メソッドを呼び出し、スレッドCがthree()メソッドを呼び出すことを示します.正しい出力は「onetwothree」です.例2:
入力:[1,3,2]出力:[onetwothree]解釈:入力[1,3,2]はスレッドAがone()メソッドを呼び出し、スレッドBがthree()メソッドを呼び出し、スレッドCがtwo()メソッドを呼び出すことを示す.正しい出力は「onetwothree」です. 
注意:
入力された数字は順序を示すようですが、オペレーティングシステムでのスレッドのスケジューリング順序は保証されていません.
入力フォーマットは主にテストの全面性を確保するために表示されます.
class Foo {

    public Foo() {
        
    }

    public void first(Runnable printFirst) throws InterruptedException {
        
        // printFirst.run() outputs "first". Do not change or remove this line.
        printFirst.run();
    }

    public void second(Runnable printSecond) throws InterruptedException {
        
        // printSecond.run() outputs "second". Do not change or remove this line.
        printSecond.run();
    }

    public void third(Runnable printThird) throws InterruptedException {
        
        // printThird.run() outputs "third". Do not change or remove this line.
        printThird.run();
    }
}