力扣1114.按序打印-----题目解析

 题目描述

力扣1114.按序打印-----题目解析_第1张图片

 力扣1114.按序打印-----题目解析_第2张图片

解析:

class Foo {
    public int a = 0;
    public Foo() {
        
    }

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

    public void second(Runnable printSecond) throws InterruptedException {
        while(true){
            if(a == 1){
                printSecond.run();
                a++;
                break;
            }else{
                Thread.yield();
            }
        }
        // printSecond.run() outputs "second". Do not change or remove this line.
        
    }

    public void third(Runnable printThird) throws InterruptedException {
        while(true){
            if(a == 2){
                printThird.run();
                break;
            }else{
                Thread.yield();
            }
        }
        // printThird.run() outputs "third". Do not change or remove this line.
        
    }
}

你可能感兴趣的:(leetcode,算法,职场和发展)