Hi everyone...
I've write a program with java that simulate production water with synchronization (Oxygen wait for Hydrogen to become available), but it's gives "Unexpected Operation Exeption" and did'nt work... Please help me...
there are my codes:
// class for thread Oxygen public class Thread_O implements Runnable {
Object object;
public Thread_O(Object o) {
object = o;
}
public void run() {
try {
oxygen();
} catch (InterruptedException ex) {
Logger.getLogger(Thread_O.class.getName()).log(Level.SEVERE, null, ex);
}
throw new UnsupportedOperationException("Not supported yet.");
}
public void oxygen() throws InterruptedException {
System.out.println("One O2 created...");
synchronized (object) {
object.wait();
object.wait();
}
System.out.println("**** H2O created...");
}
}
// class for thread hydrogen public class Thread_H implements Runnable {
Object object;
public Thread_H(Object o) {
object = o;
}
public void run() {
try {
Hydrogen();
} catch (InterruptedException ex) {
Logger.getLogger(Thread_H.class.getName()).log(Level.SEVERE, null, ex);
}
throw new UnsupportedOperationException("Not supported yet.");
}
public void Hydrogen() throws InterruptedException {
System.out.println("One H2 created...");
synchronized (object) {
object.notifyAll();
}
}
}
// in the main class we have:
Object object = new Object();
// in the button of Oxygen:
Thread thread_O = new Thread(new Thread_O(object));
thread_O.run();
// in the button of Hydrogen:
Thread thread_H = new Thread(new Thread_H(object));
thread_H.run();