Computer Science Related Others Courses AvailableThe Best Codder.blogspot.com

Write a Java program that correctly implements the producer - consumer problem using the concept of inter-thread communication. (use of synchronize)

Write a Java program that correctly implements the producer - consumer problem using the concept of inter-thread communication. (use of synchronize)

Write a Java program that correctly implements the producer - consumer problem using the concept of inter-thread communication. (use of synchronize)

  1. import java.util.LinkedList;

  2. public class Main {
  3.     public static void main(String[] args) {
  4.         // Create a shared buffer
  5.         LinkedList<Integer> buffer = new LinkedList<>();
  6.         int capacity = 5;

  7.         // Create the producer and consumer threads
  8.         Thread producer = new Thread(new Producer(buffer, capacity));
  9.         Thread consumer = new Thread(new Consumer(buffer, capacity));

  10.         // Start the threads
  11.         producer.start();
  12.         consumer.start();
  13.     }
  14. }

  15. // The producer thread that adds items to the buffer
  16. class Producer implements Runnable {
  17.     private LinkedList<Integer> buffer;
  18.     private int capacity;

  19.     public Producer(LinkedList<Integer> buffer, int capacity) {
  20.         this.buffer = buffer;
  21.         this.capacity = capacity;
  22.     }

  23.     public void run() {
  24.         for (int i = 1; i <= 10; i++) {
  25.             synchronized (buffer) {
  26.                 while (buffer.size() == capacity) {
  27.                     try {
  28.                         System.out.println("Buffer is full. Producer waiting...");
  29.                         buffer.wait();
  30.                     } catch (InterruptedException e) {
  31.                         e.printStackTrace();
  32.                     }
  33.                 }

  34.                 buffer.add(i);
  35.                 System.out.println("Produced: " + i);
  36.                 buffer.notifyAll();
  37.             }
  38.         }
  39.     }
  40. }

  41. // The consumer thread that removes items from the buffer
  42. class Consumer implements Runnable {
  43.     private LinkedList<Integer> buffer;
  44.     private int capacity;

  45.     public Consumer(LinkedList<Integer> buffer, int capacity) {
  46.         this.buffer = buffer;
  47.         this.capacity = capacity;
  48.     }

  49.     public void run() {
  50.         while (true) {
  51.             synchronized (buffer) {
  52.                 while (buffer.isEmpty()) {
  53.                     try {
  54.                         System.out.println("Buffer is empty. Consumer waiting...");
  55.                         buffer.wait();
  56.                     } catch (InterruptedException e) {
  57.                         e.printStackTrace();
  58.                     }
  59.                 }

  60.                 int item = buffer.remove();
  61.                 System.out.println("Consumed: " + item);
  62.                 buffer.notifyAll();
  63.             }
  64.         }
  65.     }
  66. }

Givan by -Ujjwal Matoliya

Post a Comment

© JAVA. The Best Codder All rights reserved. Distributed by