Hi everyone... I have a project fir my "Operating Systems". I need to write 2 program with java...
1) write a program that produce Water with 2 method Oxygen and Hydrogen. method Oxygen produce one Oxygen and method Hydrogen produce one hydrogen. when 2 Hydrogen and one Oxygen was existed H2O created. I must write this with with Semaphores and threads.
I've writed some code for this but it gives illegal monitor exeption... please help me to correct it...
This is my code:
// 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.getLogger(Thread_O.class.getName()).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.getLogger(Thread_H.class.getName()).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...!");
}
}
and in action of Oxygen Button:
Thread th = new Thread(new Thread_O());
th.start();
and in action of Oxygen Button:
Thread th = new Thread(new Thread_O());
th.start();
please help me...it's emergency...
2)Write the above problem with Monitors and Sychronize.