아래는 코드의 일부입니다. 나는 "알림 1"이 실제로 기다리고있는 다른 기능을 깨울 수없는 이유를 혼란스럽게 생각합니다. 하나의 스레드가 객체에 대해 동기화 된 메소드를 실행할 때 첫 번째 스레드가 객체로 완료 될 때까지 동일한 객체 블록 (실행 일시 중단)에 대해 동기화 된 메소드를 호출하는 다른 모든 스레드는 . 자바 멀티 스레딩 문제
왜 결과가되지 않습니다 : 대기, notify1, 대기 마감, notify2, . . .는 대신은 다음과 같습니다 대기, notify1, notify2, notify2, . . . notify2는 는 가 는 는 을 대기 이동 기다려 건너 대기 이동 3, 대기 완료 통지, 통지 2. . .
코드 { . . .
MultiThreadContent m;
void divideToParts(File testFile,FileInputStream fileInputStream, Object hashMachine) throws IOException, InterruptedException{
.
.
.
//run from here
m = new MultiThreadContent(fileInputStream,(int)temp23,(int) (testFile.length()%temp23), numberOfParts, hashMachine);
new Thread(new Read()).start();
m.update();
}
class Read implements Runnable{
@Override
public void run() {
try {
m.read();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class MultiThreadContent{
.
.
.
boolean preNotReady=true;
boolean updateNotFinished;
//read{
public synchronized void read() throws InterruptedException{
//initial{
readNextBlock();
preBlock=nextBlock;
read_notify();
System.out.println("notify 1");//d
if(finishedRead!=true)
{
readNextBlock();
read_wait();
}
else
return;
//}
while(finishedRead!=true){
preBlock=nextBlock;
read_notify();
System.out.println("notify 2");//d
readNextBlock();
read_wait();
}
//closing{
preBlock=nextBlock;
read_notify();
System.out.println("notify 3");//d
//}
}
void read_notify(){
preNotReady=false;
notifyAll();
}
void read_wait() throws InterruptedException{
if(updateNotFinished==true)
{
wait();
System.out.println("wait for update");//d
}
preNotReady=true;
}
//}
//update{
public synchronized void update() throws InterruptedException{
for (int i = 0; i < totalParts; i++) {
update_wait();
divideToParts_update(hashMachine, preBlock);
update_notify();
}
}
void update_notify(){
updateNotFinished=false;
notifyAll();
}
void update_wait() throws InterruptedException{
if(preNotReady){
System.out.println("wait");//d
wait();
System.out.println("wait finish");//d
}
updateNotFinished=true;
System.out.println("skip wait");//d
}
//}
}
}
'finishedRead'가 정의되지 않았습니까? 이 모든 코드입니까? – Kevin
코드의 일부입니다. 나는 notifyAll이 "notify 1"에서 기다리고있는 다른 함수를 깨울 수없는 이유를 혼란 스럽다. – micahli123