How to do it...

Perform the following steps to implement the example:

  1. Create a class named Producer and specify that it implements the Runnable interface:
        public class Producer implements Runnable { 
  1. Declare a private LinkedTransferQueue attribute parameterized with the String class named buffer:
        private LinkedTransferQueue<String> buffer; 
  1. Declare a private String attribute named name to store the name of the producer:
        private String name; 
  1. Implement the constructor of the class to initialize its attributes:
        public Producer(String name, LinkedTransferQueue<String> buffer){ 
this.name=name;
this.buffer=buffer;
}
  1. 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);
}
  1. Implement a class named Consumer and specify that it implements the Runnable interface:
        public class Consumer implements Runnable { 
  1. Declare a private LinkedTransferQueue attribute parameterized with the String class named buffer:
        private LinkedTransferQueue<String> buffer; 
  1. Declare a private String attribute named name to store the name of the consumer:
        private String name; 
  1. Implement the constructor of the class to initialize its attributes:
        public Consumer(String name, LinkedTransferQueue<String> buffer){ 
this.name=name;
this.buffer=buffer;
}
  1. 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);
}
  1. 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) {
  1. 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<>();
  1. Create an array of 100 Thread objects to execute 100 producer tasks:
        Thread producerThreads[]=new Thread[THREADS]; 
  1. Create an array of 100 Thread objects to execute 100 consumer tasks:
        Thread consumerThreads[]=new Thread[THREADS]; 
  1. 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();
}
  1. 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();
}
  1. 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();
}
}
  1. 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 ");
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.84.25