Java 生产者和消费者同步队列线程

Java synchronized queue thread on producer and consumer

Java producer-consumer 程序使用thread & synchronized queue,程序分成3个类但不能运行.

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;
    }
}

已同步Queue.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)

enqueuedequeue 方法的 catch 块中,形成 class SynchronizedQueue 您正在尝试转换 queue 成员属性这是 QueueSynchronizedQueue 类型。

SynchronizedQueue.enqueue() 中我们有:

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

由于QueueSynchronizedQueue之间没有关系,编译器给出了编译错误。你应该删除演员表。

但最好的解决方案是只使用 JAVA SDK 中可用的 java.util.concurrent.BlockingQueue 实现,它将为您处理所有同步部分。