0

我用​​和busy waiting写了这个java代码,但我不知道它为什么不能正常工作!例如我得到这个输出我的java(生产者 - 消费者)代码有什么问题?

produced:6 
produced:7 
consumed:0 
consumed:0 

6和7产生,但0如何被消耗???我不知道它有什么问题,请帮助我,这里是我的代码:

package conper; 
import java.util.*; 
import java.util.concurrent.TimeUnit; 

class Queue<Integer> { 

    int items=0; 
    int q=0; 
    public int[] array= new int[10]; 

    Queue(int i) { 

    } 
    public boolean isEmpty(){ 
     if (this.q > 0) 
     { 
      return false ; 
     }else{ 

      return true ;} 
    } 
    public boolean isFull(){ 
     if (this.q > 10) 
     { 

      return true ; 
     }else{ 

      return false ; 
     } 
    } 
    public int size(){ 
     return items; 
    } 


    public synchronized void add(int x){ 
     if(!isFull()){ 
      array[++q]=x; 


     } 
    } 
    public synchronized void remove(int x){ 
     if(!isEmpty()){ 
      array[--q]=x; 

     } 
    } 
} 
class Producer implements Runnable { 

    private Random random = new Random(); 

    private Queue<Integer> queue; 
    private boolean working = true; 


    public Producer(Queue<Integer> q) { 
     queue = q; 

    } 

    public void run() { 
     int product=0; 
     int loop = random.nextInt(10) + 20; 
     for (int i = 0; i < loop; i++) { 
      double h = Math.pow(2, Math.E * i); 

     } 

     while(working){ 
      product = produce(); 
      if(!queue.isFull()){ 
       break; 

      } 

     } 
     queue.add(product); 
     System.out.println("produced:"+product); 

    } 
    public void stop() { 
     working = false; 
    } 
     private int produce() { 
     int result = 0; 
     int loop = random.nextInt(10) + 20; 
     for (int i = 0; i < loop; i++) { 
      double h = Math.pow(2, Math.E * i); 

     } 
     result = random.nextInt(10); 
     return result; 
    } 

    } 
class Consumer implements Runnable { 

    private Random rnd = new Random(); 

    private Queue<Integer> queue; 
    private boolean working = true; 


    public Consumer(Queue<Integer> q) { 
     queue = q; 

    } 

    public void run() { 

     int product = 0; 
     while(working){ 

      if(!queue.isEmpty()) 
       break; 

     } 
     queue.remove(product); 

     System.out.println("consumed:"+product); 


     consume(product); 
    } 
    public void stop() { 
     working = false; 
    } 
    public void consume(int product) { 
     int loop = rnd.nextInt(10000) + 2000; 
     for (int i = 0; i < loop; i++) { 
      double h = Math.pow(2, Math.E * i); 
     } 

    } 
} 
public class Main { 
    public static void main(String[] args) { 

     Queue<Integer> queue = new Queue<Integer>(5); 

     Producer p1 = new Producer(queue); 
     Producer p2 = new Producer(queue); 

     Consumer c1 = new Consumer(queue); 
     Consumer c2 = new Consumer(queue); 



     Thread pt1 = new Thread(p1); 
     Thread pt2 = new Thread(p2); 

     Thread ct1 = new Thread(c1); 
     Thread ct2 = new Thread(c2); 


     pt1.start(); 
     pt2.start(); 

     ct1.start(); 
     ct2.start(); 

     try { 
      TimeUnit.MILLISECONDS.sleep(200); 

      p1.stop(); 
      p2.stop(); 

      c1.stop(); 
      c2.stop(); 
     } 

     catch (Exception e) { 
      return; 
     } 

    } 


    } 

有什么建议吗?

+0

这是它满时'Q> = 10' –

+0

的'--q'是'Q ++' –

+0

只使用相对'的Thread.join()'代替'使用Thread.stop();' –

回答

1

你的消费者的代码读取

int product = 0; 
// code which doesn't change "product" 
System.out.println("consumed:"+product); 

我建议你需要一个remove()方法,其返回从队列中,而不是传递一个值的值。

我还建议您在尝试在多个线程中使用之前,先让代码在一个线程中工作。

+0

我该如何更改我的删除方法? –

+0

@blackrose你的意思是;我如何返回一个值?我怎么不看价值? –

+0

public synchronized int remove(int x){(isEmpty()){ array [-q] = x; } return x; }你的意思是? –