難度 簡(jiǎn)單
低速通過(guò),涉及線程的還不是很熟,后面需要系統(tǒng)看下線程的。
執(zhí)行用時(shí) :18 ms, 在所有 Java 提交中擊敗了6.70%的用戶
內(nèi)存消耗 :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();
}
}