我們提供了乙個類:
public class foo
public void two()
public void three()
}三個不同的執行緒將會共用乙個 foo 例項。
執行緒 a 將會呼叫 one() 方法
執行緒 b 將會呼叫 two() 方法
執行緒 c 將會呼叫 three() 方法
請設計修改程式,以確保 two() 方法在 one() 方法之後被執行,three() 方法在 two() 方法之後被執行。
示例 1:
輸入: [1,2,3]
輸出: 「onetwothree」
解釋:有三個執行緒會被非同步啟動。
輸入 [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」。
package lt;
class foo
private boolean firstfinished;
private boolean twofinished;
object lock = new object();
public void one()
public void two()
public void three()
public void first(runnable printfirst) throws interruptedexception
}public void second(runnable printsecond) throws interruptedexception
printsecond.run();
twofinished = true;
lock.notifyall();}}
public void third(runnable printthird) throws interruptedexception
printthird.run();}}
public static void main(string args) throws interruptedexception catch (interruptedexception e)
}});
thread thread2 = new thread(new runnable() catch (interruptedexception e)
}});
thread thread3 = new thread(new runnable() catch (interruptedexception e)
}});
thread1.start();
thread2.start();
thread3.start();
}}
寫了好久的main方法。讓人心塞。還是掌握的不牢固。runnable,thread都要手動 start才是非同步執行 Leetcode 1114 按序列印
我們提供了乙個類 public class foo public void two public void three 三個不同的執行緒將會共用乙個 foo 例項。執行緒 a 將會呼叫 one 方法 執行緒 b 將會呼叫 two 方法 執行緒 c 將會呼叫 three 方法 請設計修改程式,以確保 ...
leetcode1114 按序列印
我們提供了乙個類 public class foo public void two public void three 三個不同的執行緒將會共用乙個 foo 例項。執行緒 a 將會呼叫 one 方法 執行緒 b 將會呼叫 two 方法 執行緒 c 將會呼叫 three 方法 請設計修改程式,以確保 ...
Leetcode刷題1114 按序列印
我們提供了乙個類 public class foo public void second public void third 三個不同的執行緒將會共用乙個 foo 例項。執行緒 a 將會呼叫 first 方法 執行緒 b 將會呼叫 second 方法 執行緒 c 將會呼叫 third 方法 請設計修...