如何解决消费者的问题?

问题描述 投票:0回答:1

我想用ReentrantLock实现一个有仓库、消费者和制造商的程序。我有一个基于数组的循环队列作为货物的存储。生产者最多可以生产n次产品。我面临的问题是,消费者不参与这个过程。你能告诉我,我哪里做错了吗?

package com.company;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.Condition;

public class Storage {
private final int q[];
private int head,tail,switcher;
ReentrantLock lock= new ReentrantLock();;
Condition notfull = lock.newCondition();
Condition notempty = lock.newCondition();

public Storage(int size) {
    q = new int[size];
    head = 0;
    tail = 0;
}

public boolean isFull(){
    return ((tail == q.length-2)&&(head==0))||(tail+1==head);
}

public boolean isEmpty(){ 
    return ((tail==q.length-1)&&(head==0))||(head+1==tail)||(head+1==0);
}

public int put(int el){ 
    lock.lock();
    try {
        while (isFull()){
            notfull.await();
        }
        q[tail]=el;
        System.out.print("PRODUCER ADDED: "+el);
        System.out.print("STATUS OF STORAGE: ");
        print();
        System.out.println();
        System.out.println("head = "+head);
        System.out.println("tail = "+tail);
        notempty.signalAll();
    }
    catch (InterruptedException e){}
    finally {
        lock.unlock();
        return el;
    }
}

public int get(){
    lock.lock();
    try {
        while (isEmpty()) notempty.await();
        head = step(head);
        System.out.print("CONSUMER GOT: "+q[head]);
        System.out.print("STATUS OF STORAGE: ");
        print();
        System.out.println();
        System.out.println("head = "+head);
        System.out.println("tail = "+tail);
        notfull.signalAll();
    }
    catch (InterruptedException e){}
    finally {
        lock.unlock();
        return q[head];
    }

}

public int step(int i){
    int s;
    s=(i+1)%q.length;
    return s;
}

public void print() {
    int i;
    for (i = head; i != tail; i = step(i)) {
        System.out.print(q[i]+" ");
    }
  }
}

生产者类

public class Producer implements Runnable {
protected int n =10; // limit of producing
private int el=1; //
private Storage q;
Thread p;

public Producer(Storage q){
    this.q = q;
    p=new Thread(this, "Producer");
}


public void run() {
    for (int i=1;i<n;i++){
        q.put(el++);
        try {
            p.sleep(2000);
        }
        catch (InterruptedException e){}
        }
    }
   }

消费类

public class Consumer implements Runnable {
private Storage q;
static Thread c;

public Consumer(Storage q){
    this.q = q;
    c=new Thread(this, "Producer");
}

@Override
public void run(){
      for (int i=1; i<10;i++){
          try {
              c.sleep(1000);
          } catch (InterruptedException e){}
          q.get();
      }
    }
  }

主类

public class Main {
public static void main(String[] args) {
    Storage q = new Storage(6); // общий склад
    Producer producer = new Producer(q);
    Consumer consumer = new Consumer(q);
    new Thread(producer).start();
    new Thread(consumer);
 }

}

multithreading producer-consumer reentrantlock
1个回答
0
投票

首先,在你的主类中,你忘了通过调用start来启动消费者线程。

new Thread(consumer).start();

其次,我认为你也有一个逻辑问题,你没有在put上改变你的尾巴。我不确定这是否是故意的,但以上的改变应该能解决你的问题。

© www.soinside.com 2019 - 2024. All rights reserved.