2

I want to implement Circular Buffer using 2 threads. One Thread that reads from the buffer and the other one writes to the buffer as follow: enter image description here

Assuming that process P is the writer process and process Q is the reader process.

After starting both processes from the main class, how can I pass to process P (the writer) the value I want it to write to the buffer each time I want to write to the buffer? Similarly, how can I ask process Q (the reader) to read from the buffer (i.e. how can I call it to return the value that it has read from the buffer)?

I am confused because the implementation of both processes is done in the run() method and this method is executed when we issue the .start() command. However, once started, how can we keep passing and reading parameters while it is in the run mode.

I am following the implementation in the following example mentioned in the following question : Circular Buffer with Threads Consumer and Producer: it get stucks some executions

Shared Variables:

public class BufferCircular {
    volatile int[] array;
    volatile int p;
    volatile int c;
    volatile int nElem;

    public BufferCircular(int[] array) {
       this.array = array;
       this.p = 0;
       this.c = 0;
       this.nElem = 0;
    }

    public void writeData (int data) {
       this.array[p] = data;
       this.p = (p + 1) % array.length;
       this.nElem++;
    }

    public int readData() {
       int data = array[c];
       this.c = (c + 1) % array.length;
       this.nElem--;
       return data;
    }
}

Writer Process:

public class Producer extends Thread {
    BufferCircular buffer;
    int bufferTam;
    int contData;

    public Productor(BufferCircular buff) {
       this.buffer = buff;
       this.bufferTam = buffer.array.length;
       this.contData = 0;
    }

    public void produceData() {
       this.contData++;
       this.buffer.writeData(contData);
    }

    public void run() {
       for (int i = 0; i < 500; i++) {
          while (this.buffer.nElem == this.bufferTam) {
              Thread.yield();
          }
          this.produceData();
       }
    }
}

Reader Process:

public class Consumer extends Thread {
    BufferCircular buffer;
    int cont;

    public Consumer(BufferCircular buff) {
       this.buffer = buff;
       this.cont = 0;
    }

    public void consumeData() {
       int data = buffer.readData();
       cont++;
       System.out.println("data  " + cont + ": " + data);
    }

    public void run() {
       for (int i = 0; i < 500; i++) {
          while (this.buffer.nElem == 0) {
             Thread.yield();
          }
          this.consumeData();
       }
    }
}

Main:

public class Main {

    public static void main(String[] args) {
       Random ran = new Random();
       int tamArray = ran.nextInt(21) + 1;
       int[] array = new int[tamArray];

       BufferCircular buffer = new BufferCircular(array);

       Producer producer = new Producer (buffer);
       Consumer consumer = new Consumer (buffer);

       producer.start();
       consumer.start();

       try {
           producer.join();
           consumer.join();
       } catch (InterruptedException e) {
           System.err.println("Error with Threads");
           e.printStackTrace();
       }

    }
}
frido
  • 13,065
  • 5
  • 42
  • 56
Jan
  • 747
  • 1
  • 10
  • 29

1 Answers1

1

You main thread should not be responsible for passing value to producer thread and in same way it should not be responsible to print the data from consumer.

Producer: this should be responsible to getting the data and inserting into your queue, in your case its currently increment int value and passing it, but you can also read data from file or db or use std input to take user input and pass that data in the queue.
Consumer: this should be responsible to process the data from the queue in ur case printing the numbers.

Check online

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
  • How can I make Producer insert into queue after starting the thread. How would I call it with the parameters and what modification should I do to the above methods? – Jan Aug 07 '19 at 10:27
  • @Jan what problem you are getting when you run this program? – Sumit Singh Aug 07 '19 at 10:31
  • I am not getting any problem at the moment. However, I want to use it for another task where I will be reading from file and passing parameters to those reading and writing threads. The current implementation does not allow me to proceed with the next task where I will need these functions to work. – Jan Aug 07 '19 at 10:34
  • For that 1 option you can create FileReader in your Producer constructor and from produceData() you can read the data let say line by line and put it in queue, better use queue implementation from java see https://www.mkyong.com/java/java-blockingqueue-examples/ – Sumit Singh Aug 07 '19 at 10:40