2017-08-08 3 views
-2
public class Queue { 
    int value = 0; 
    boolean isEmpty = true; 
    public synchronized void put(int n){ 
     if (!isEmpty){ 
      try { 
       System.out.println("producer is waiting"); 
       wait(); 
      }catch (Exception e){ 
       e.printStackTrace(); 
      } 
     } 
     value += n; 
     isEmpty = false; 
     System.out.println("The number of products:"+value); 
     notifyAll(); 
    } 
    public synchronized void get(){ 
     if (isEmpty){ 
      try{ 
       System.out.println("customer is waiting"); 
       wait(); 
      }catch (Exception e){ 
       e.printStackTrace(); 
      } 
     } 
     value --; 
     if(value<1){ 
      isEmpty = true; 
     } 
     System.out.println("there are "+value+" left"); 
     notifyAll(); 
    } 
} 


public class Producer extends Thread{ 
    private Queue queue; 
    public Producer(String name,Queue queue){ 
     super(name); 
     this.queue = queue; 
    } 

    @Override 
    public void run() { 
     for (int i = 0; i < 5; i++) { 
      queue.put(i+1); 
     } 
    } 

} 

public class Producer extends Thread{ 
    private Queue queue; 
    public Producer(String name,Queue queue){ 
     super(name); 
     this.queue = queue; 
    } 

    @Override 
    public void run() { 
     for (int i = 0; i < 5; i++) { 
      queue.put(i+1); 
     } 
    } 
} 


public class Main { 

    public static void main(String[] args) { 
     Queue queue = new Queue(); 
     Thread customer1 = new Customer("customer1",queue); 
     Thread producer1 = new Producer("producer1",queue); 
     customer1.setPriority(4); 
     producer1.setPriority(7); 
     producer1.start(); 
     customer1.start(); 

    } 

} 

Ausgänge:Java Verbraucher/Produzent

 
The number of products:1  
producer is waiting  
there are 0left  
customer is waiting  
The number of products:2  
producer is waiting  
there are 1left  
there are 0left  
customer is waiting  
The number of products:3  
producer is waiting  
there are 2left  
there are 1left  
there are 0left  
customer is waiting 
The number of products:4  
producer is waiting  
there are 3left  
there are 2left  
there are 1left  
there are 0left  
customer is waiting  
The number of products:5  
there are 4left  
there are 3left  
there are 2left  
there are 1left  
there are 0left  
customer is waiting 

Ich weiß nicht, warum die Reihenfolge der Ausgänge wie diese sind.

Warum nicht, es gibt das Ergebnis wie

 
The number of products:3 
producer is waiting  
there are 2left  
The number of products: 6  
there are 5left    
The number of products: 10  
there are 9left  
The number of products: 15   
there are 14left 
+0

Warum wollen Sie das zu Multi-Thread müssen? – Professor901

+0

Ich möchte nur wissen, warum es so nicht funktioniert. –

+0

Prozesssynchronisation ist nicht garantiert. –

Antwort

0

ich glaube, das Problem ist, dass Sie nicht für Ihren Zustand überprüfen nach benachrichtigt beeing.

Sie tun ein

if (condition){ 
    wait(); 
} 
increment(); 

Während der Thread von einem anderen Zusammenhang mitgeteilt werden könnte, wird es nicht überprüfen, ob die Bedingung noch existiert oder wurde von einem anderen Thread und tun den Zuwachs ohnehin gelöst. Richtige wäre Ihr Zustand auf aufwachen noch einmal zu überprüfen, so etwas wie:

while (condition){ 
    wait(); 
} 
increment();