2016-02-27 152 views
0

Java生產者 - 消費者程序使用線程&同步隊列,程序被分成3個類但它不能運行。生產者和消費者的Java同步隊列線程

Queue.java:

public class Queue { 
    static final int MAXQUEUE = 3; 
    int[] queue = new int[MAXQUEUE]; 
    int front, rear; 
    public Queue(){ front = 0; rear = 0; } 
    public boolean isEmpty(){ return (front==rear); } 

    public boolean isFull(){ 
     int index = rear+1 < MAXQUEUE ? rear+1 : 0; 
     return (index == front); 
    } 

    public void enqueue(int value) { 
     queue[rear] = value; 
     rear = rear+1 < MAXQUEUE ? rear+1 : 0; 
    } 

    public int dequeue(){ 
     int data = queue[front]; 
     front = front+1 < MAXQUEUE ? rear+1 : 0; 
     return data; 
    } 
} 

SynchronizedQueue.java:

import java.util.Queue; 

public class SynchronizedQueue { 
    Queue queue; 
    public SynchronizedQueue() {queue = new Queue(); } 
    public synchronized void enqueue(int value) { 
     try { 
      while (queue.isFull()) { 
       System.out.println(); 
       System.out.println("Queue is full, please wait...."); 
       wait(); 
      } 
     } 
     catch (InterruptedException e) { } 
     ((SynchronizedQueue) queue).enqueue(value); 
     notify(); 
    } 
    public synchronized int dequeue() { 
     try { 
      while (queue.isEmpty()) { 
       System.out.println(); 
       System.out.println("Queue is empty, please wait...."); 
       wait(); 
      } 
     } 
     catch (InterruptedException e) { } 
     int data = ((SynchronizedQueue) queue).dequeue(); 
     notify(); 
     return data; 
    } 
} 

主程序Ch10_3.java:

class Producer extends Thread { 
    public int count = 0; 
    public void run() { 
     int value; 
     while (Ch10_3.isRunning) { 
      value = (int)(Math.random()*100); 
      Ch10_3.squeue.enqueue(value); 
      System.out.print(">" + value + "]"); 
      count++; 
      try { 
       Thread.sleep((int)(Math.random()*100)); 
      } 
      catch(InterruptedException e) { } 
     } 
     System.out.println("\n" + Thread.currentThread() + "Producer thread end."); 
    } 
} 

class Consumer extends Thread { 
    public int count = 0; 
    public void run() { 
     int data; 
     while (Ch10_3.isRunning) { 
      data = Ch10_3.squeue.dequeue(); 
      System.out.println("[" + data + ">"); 
      count++; 
      try { 
       Thread.sleep((int)(Math.random()*100)); 
      } 
      catch(InterruptedException e) { } 
     } 
     System.out.println("\n" + Thread.currentThread() + "Consumer thread end."); 
    } 
} 

public class Ch10_3 { 
    static final int MAXITEMS = 10; 
    static SynchonizedQueue squeue = new SynchronizedQueue(); 
    static boolean isRunning = true; 

    public static void main(String[] args) { 
     Producer producer = new Producer(); 
     Consumer consumer = new Consumer(); 

     producer.start(); consumer.start(); 
     while (true) 
      if (producer.count >= MAXITEMS && producer.count == consumer.count) 
      { isRunning = false; break; } 
    } 
} 

錯誤消息:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: at Ch10_3.main(Ch10_3.java:41)

+0

哪行代碼是Ch10_3.java的第41行? – VGR

+0

'static SynchonizedQueue squeue = new SynchronizedQueue();' – Snowman

+0

'你拼錯'SynchonizedQueue'寫'SynchronizedQueue'。 –

回答

0

在從enqueuedequeue方法catch塊構成SynchronizedQueue你試圖鑄queue構件屬性,是Queue類型的,以SynchronizedQueue類。

SynchronizedQueue.enqueue()我們:

((SynchronizedQueue) queue).enqueue(value); 

既然有QueueSynchronizedQueue編譯器之間沒有關係給出一個編譯錯誤。你應該刪除演員。

但是,最好的解決方案是使用JAVA SDK中提供的java.util.concurrent.BlockingQueue實現,它將爲您處理所有的同步部分。

+0

可能包含新語句'@SuppressWarnings(「rawtypes」)'help?錯誤消息「can not instantiate type Queue」for this statement'public SynchronizedQueue(){queue = new Queue(); }'和「此方法isFull()未定義類型Queue」for'while(queue.isFull()){' – Snowman

+1

它不會幫助,一般情況下,除非您確切知道您正在做,而且你不想在編譯過程中打擾他們。您是否更新了代碼,與您最初發布的代碼相比較,因爲錯誤消息非常奇怪? – iullianr

+0

哦,是的,刪除壓制語句,並得到錯誤消息:線程「main」中的_Exception java.lang.Error:未解決的編譯問題:無法實例化類型Queue方法isFull()對於SynchronizedQueue類型Queue未定義。在Ch10_3處有(SynchronizedQueue.java:5)。 (Ch10_3.java:38)_ – Snowman