Perform the following steps to implement the example:
- Create a class named Producer and specify that it implements the Runnable interface:
public class Producer implements Runnable {
- Declare a private LinkedTransferQueue attribute parameterized with the String class named buffer:
private LinkedTransferQueue<String> buffer;
- Declare a private String attribute named name to store the name of the producer:
private String name;
- Implement the constructor of the class to initialize its attributes:
public Producer(String name, LinkedTransferQueue<String> buffer){
this.name=name;
this.buffer=buffer;
}
- Implement the run() method. Store 10,000 strings in the buffer using the put() method of the buffer object and write a message to the console indicating the end of the method:
@Override
public void run() {
for (int i=0; i<10000; i++) {
buffer.put(name+": Element "+i);
}
System.out.printf("Producer: %s: Producer done ",name);
}
- Implement a class named Consumer and specify that it implements the Runnable interface:
public class Consumer implements Runnable {
- Declare a private LinkedTransferQueue attribute parameterized with the String class named buffer:
private LinkedTransferQueue<String> buffer;
- Declare a private String attribute named name to store the name of the consumer:
private String name;
- Implement the constructor of the class to initialize its attributes:
public Consumer(String name, LinkedTransferQueue<String> buffer){
this.name=name;
this.buffer=buffer;
}
- Implement the run() method. Take out 10,000 strings from the buffer using the take() method of the buffer object and write a message to the console indicating the end of the method:
@Override
public void run() {
for (int i=0; i<10000; i++){
try {
buffer.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.printf("Consumer: %s: Consumer done ",name);
}
- Implement the main class of the example. Create a class named Main and add to it the main() method:
public class Main {
public static void main(String[] args) {
- Declare a constant named THREADS and assign to it the value 100. Create a LinkedTransferQueue object with the String class object and call it buffer:
final int THREADS=100;
LinkedTransferQueue<String> buffer=new LinkedTransferQueue<>();
- Create an array of 100 Thread objects to execute 100 producer tasks:
Thread producerThreads[]=new Thread[THREADS];
- Create an array of 100 Thread objects to execute 100 consumer tasks:
Thread consumerThreads[]=new Thread[THREADS];
- Create and launch 100 Consumer objects and store the threads in the array created earlier:
for (int i=0; i<THREADS; i++){
Consumer consumer=new Consumer("Consumer "+i,buffer);
consumerThreads[i]=new Thread(consumer);
consumerThreads[i].start();
}
- Create and launch 100 Producer objects and store the threads in the array created earlier:
for (int i=0; i<THREADS; i++) {
Producer producer=new Producer("Producer: "+ i , buffer);
producerThreads[i]=new Thread(producer);
producerThreads[i].start();
}
- Wait for the finalization of the threads using the join() method:
for (int i=0; i<THREADS; i++){
try {
producerThreads[i].join();
consumerThreads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
- Write a message to the console with the size of the buffer:
System.out.printf("Main: Size of the buffer: %d ",
buffer.size());
System.out.printf("Main: End of the example ");