나는 동기화 (수소가 사용 가능하게 될 때까지 기다림)와 함께 생산 수를 시뮬레이트하는 java로 프로그램을 작성했지만 "예기치 않은 연산 Exeption"을 제공하고 작동하지 않는다. 자바의 동기화 된 메소드에서 도움이
내 코드가 있습니다 ... 제발 도와주세요 : 스레드 산소에 대한 이// 클래스 공용 클래스 Thread_O는 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...");
}
}
구현 스레드 수소 공용 클래스 Thread_H에 대한가 // 클래스는 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();
}
}
}
// 기본 클래스에서 우리가 가진 :
개체 개체 = 새 개체();
// 산소의 버튼에서 :
Thread thread_O = new Thread(new Thread_O(object));
thread_O.run();
// 수소의 버튼에서 :
Thread thread_H = new Thread(new Thread_H(object));
thread_H.run();
중복? http://stackoverflow.com/questions/1978893/java-semaphore-and-monitor – trashgod
와우, 3 개의 복제 스레드, 다른 하나의 복제본이기 때문에 하나가 닫혔습니다. –