难度 简单
低速通过,涉及线程的还不是很熟,后面需要系统看下线程的。
执行用时 :18 ms, 在所有 Java 提交中击败了6.70%的用户
内存消耗 :39.1 MB, 在所有 Java 提交中击败了5.09%的用户
int mark = 0;
Object lock = new Object();
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
synchronized(lock){
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
mark = 1;
lock.notifyAll();
}
}
public void second(Runnable printSecond) throws InterruptedException {
synchronized(lock){
while(mark != 1){
lock.wait();
}
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
mark = 2;
lock.notifyAll();
}
}
public void third(Runnable printThird) throws InterruptedException {
synchronized(lock){
while(mark != 2){
lock.wait();
}
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
lock.notifyAll();
}
}