我有一個針對「操作系統」的項目。我需要用java編寫2個程序...IllegalMonitorException在Java中使用信號量和監視器
編寫一個程序,用2種方法產生水和氫。方法氧氣產生一種氧氣和方法氫氣產生一種氫氣。當2個氫氣和一個氧氣存在時,H2O產生。我必須用信號量和線程來寫這個。
用Monitors和Sychronize寫上面的問題。
我所著的一些代碼,這一點,但它給非法監視exeption ...... 請幫我糾正......
這是我的代碼:
// class for implement Thread for oxygen
public class Thread_O implements Runnable {
public void run() {
thread t = new thread();
try {
t.oxygen();
} catch (InterruptedException ex) {
Logger logger = Logger.getLogger(Thread_O.class.getName());
logger.log(Level.SEVERE, null, ex);
}
}
}
// class for implement Thread for Hydrogen
public class Thread_H implements Runnable {
public void run() {
thread t = new thread();
try {
t.Hydrogen();
} catch (InterruptedException ex) {
Logger logger = Logger.getLogger(Thread_H.class.getName());
logger.log(Level.SEVERE, null, ex);
}
}
}
//class for method Oxygen and Hydrogen
public class thread {
Semaphore O = new Semaphore(0, true);
Semaphore H = new Semaphore(0, true);
Semaphore H2O = new Semaphore(0, true);
Semaphore safe = new Semaphore(1, true);
public void oxygen() throws InterruptedException {
safe.wait();
H.wait();
H.wait();
H2O.release();
H2O.release();
Safe.release();
// System.out.println("O2...!");
}
public void Hydrogen() throws InterruptedException {
H.release();
H2O.wait();
// System.out.println("H2...!");
}
}
並在氧氣按鈕的作用:
Thread th = new Thread(new Thread_O());
th.start();
你或許可以期待有用的指向正確的方向,但不是有人完成你的任務。 也許你應該從看文本開始,看起來像你在底部複製並粘貼了相同的文本,你是否留下了一些東西? – Fredrik 2009-12-30 06:41:08
順便說一句,你得到例外的原因是你沒有在等待什麼同步。看看這個:http://www.jchq.net/tutorial/07_03Tut.htm – Fredrik 2009-12-30 06:42:06
緊急情況下,你的意思是你拖延了自己的任務直到最後一分鐘? – 2009-12-30 06:48:01