【问题标题】:How can I solve the problem with the consumer?如何解决消费者的问题?
【发布时间】:2020-05-02 10:17:26
【问题描述】:

我正在尝试使用 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】:

    首先在您的主类中,您忘记通过调用 start 来启动 Consumer 线程。

    new Thread(consumer).start();
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 2020-06-28
      相关资源
      最近更新 更多