public class test {
boolean val
def obj=new Object()
def dos() {
val=false
Thread.start() {
synchronized(obj) {
val=true
obj.notifyAll()
}
}
Thread.sleep(5000)
synchronized(obj) {
while (!val) {
obj.wait()
}
}
}
static void main(String[] args) {
def t=new test()
t.dos()
}
}
확인 그루비는/usr/빈/ENV, 여기에 더 자세히 내 문제입니다.
스레드 (A) 별도의 스레드에서 작업을 시작한 다음 완료 될 때까지 기다립니다. - 그렇지 않으면 thread.join()을 사용하면 틀림없이 참입니다. 이 스레드는 실제로 다음 조치를
class A {
private boolean finished
public synchronized void methodOne() {
finished=true;
notifyAll();
}
public void methodTwo() {
new ThreadThatCallsMethodOneWhenDone().start();
synchronized(this) {
while (!finished) {
wait();
}
}
}
}
완성 된이 코드는 괜찮 때 우리가 신호를 얻거나 아직도 잠재적 인 문제로 실행하고 결국 methodOne
나사 (B) 신호 작업을 시작? 무엇이 더 좋은 방법으로 해결할 수 있습니까? 올
미샤
궁금 :
옵션 1 개
class A {
public void methodOne() {
synchronized(this) {
modifyvalue
notifyAll()
}
}
public void methodTwo() {
while (valuenotmodified) {
synchronized(this) {
wait()
}
}
}
옵션 2
class A {
public void methodOne() {
modifyvalue
synchronized(this) {
notifyAll()
}
}
public void methodTwo() {
while (valuenotmodified) {
synchronized(this) {
wait()
}
}
}
그 이유는 무엇입니까?
21 개의 질문, 3 개의 답변이 허용되었습니다. 그들 모두 정말 그렇게 나빴나요? – danben