Der Code geht in den Deadlock. Selbst wenn es zum Producer-Teil geht, sobald es wait()
erreicht, geht es in Deadlock. Aus meiner Sicht, wenn wait()
getroffen wird, sollte es zu Consumer-Thread gehen, anstatt in Deadlock zu gehen.Mein Producer Consumer-Code wird in Deadlock gehen
package com.java.thread.self.practice;
public class Producer_Consumer {
private volatile boolean prodFlag = true;
private volatile boolean consFlag = false;
public static void main(String[] args){
Producer_Consumer producer_Consumer = new Producer_Consumer();
producer_Consumer.startThreads();
}
private void startThreads() {
Thread producer = new Thread(new Runnable(){
@Override
public void run() {
while(true){
try {
System.out.println("Before Producer invocation :::::: ");
producer();
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
Thread consumer = new Thread(new Runnable(){
@Override
public void run() {
while(true){
try {
System.out.println("Before Consumer invocation :::::: ");
consumer();
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
producer.start();
consumer.start();
}
void producer() throws InterruptedException {
System.out.println("Inside the producer method ::::: "+this.getClass());
synchronized(this){
if(prodFlag){
System.out.println("PRODUCE !!!");
consFlag = true;
System.out.println("Before calling wait in producer :::::: ");
notify();
wait();
System.out.println("After calling wait in producer :::::: ");
}else{
System.out.println("Before calling notify in producer :::::: ");
consFlag = true;
wait();
System.out.println("After calling notify in producer :::::: ");
}
}
}
void consumer() throws InterruptedException {
System.out.println("Inside the consumer method ::::: "+this.getClass());
synchronized(this){
if(consFlag){
System.out.println("CONSUME !!!");
prodFlag = true;
System.out.println("Before calling wait in consumer :::::: ");
notify();
wait();
System.out.println("After calling wait in consumer :::::: ");
}else{
System.out.println("Before calling notify in consumer :::::: ");
prodFlag = true;
wait();
System.out.println("After calling wait in consumer :::::: ");
}
}
}
}
Ich habe Ihren Code getestet und es blockiert nicht. Er druckt kontinuierlich "Produce" "Consume" in der Konsole – Flood2d
Versuchen Sie im Debug-Modus. –
Was passiert, wenn sowohl Ihr Producer-Thread als auch Ihr Consumer-Thread 'notify()' aufrufen und dann beide 'wait()' aufrufen? Was dachtest du, würdest du einen von ihnen wieder aufwecken? –