|
| 1 | +package multithreading; |
| 2 | + |
| 3 | +//Java program to implement solution of producer |
| 4 | +//consumer problem. |
| 5 | + |
| 6 | +import java.util.LinkedList; |
| 7 | + |
| 8 | +public class Threadexample { |
| 9 | + public static void main(String[] args) |
| 10 | + throws InterruptedException |
| 11 | + { |
| 12 | + // Object of a class that has both produce() |
| 13 | + // and consume() methods |
| 14 | + final PC pc = new PC(); |
| 15 | + |
| 16 | + // Create producer thread |
| 17 | + Thread t1 = new Thread(new Runnable() { |
| 18 | + @Override |
| 19 | + public void run() |
| 20 | + { |
| 21 | + try { |
| 22 | + pc.produce(); |
| 23 | + } |
| 24 | + catch (InterruptedException e) { |
| 25 | + e.printStackTrace(); |
| 26 | + } |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + // Create consumer thread |
| 31 | + Thread t2 = new Thread(new Runnable() { |
| 32 | + @Override |
| 33 | + public void run() |
| 34 | + { |
| 35 | + try { |
| 36 | + pc.consume(); |
| 37 | + } |
| 38 | + catch (InterruptedException e) { |
| 39 | + e.printStackTrace(); |
| 40 | + } |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + // Start both threads |
| 45 | + t1.start(); |
| 46 | + t2.start(); |
| 47 | + |
| 48 | + // t1 finishes before t2 |
| 49 | + t1.join(); |
| 50 | + t2.join(); |
| 51 | + } |
| 52 | + |
| 53 | + // This class has a list, producer (adds items to list |
| 54 | + // and consumer (removes items). |
| 55 | + public static class PC { |
| 56 | + |
| 57 | + // Create a list shared by producer and consumer |
| 58 | + // Size of list is 2. |
| 59 | + LinkedList<Integer> list = new LinkedList<>(); |
| 60 | + int capacity = 2; |
| 61 | + |
| 62 | + // Function called by producer thread |
| 63 | + public void produce() throws InterruptedException |
| 64 | + { |
| 65 | + int value = 0; |
| 66 | + while (true) { |
| 67 | + synchronized (this) |
| 68 | + { |
| 69 | + // producer thread waits while list |
| 70 | + // is full |
| 71 | + while (list.size() == capacity) |
| 72 | + wait(); |
| 73 | + |
| 74 | + System.out.println("Producer produced-" |
| 75 | + + value); |
| 76 | + |
| 77 | + // to insert the jobs in the list |
| 78 | + list.add(value++); |
| 79 | + |
| 80 | + // notifies the consumer thread that |
| 81 | + // now it can start consuming |
| 82 | + notify(); |
| 83 | + |
| 84 | + // makes the working of program easier |
| 85 | + // to understand |
| 86 | + Thread.sleep(1000); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Function called by consumer thread |
| 92 | + public void consume() throws InterruptedException |
| 93 | + { |
| 94 | + while (true) { |
| 95 | + synchronized (this) |
| 96 | + { |
| 97 | + // consumer thread waits while list |
| 98 | + // is empty |
| 99 | + while (list.size() == 0) |
| 100 | + wait(); |
| 101 | + |
| 102 | + // to retrieve the first job in the list |
| 103 | + int val = list.removeFirst(); |
| 104 | + |
| 105 | + System.out.println("Consumer consumed-" |
| 106 | + + val); |
| 107 | + |
| 108 | + // Wake up producer thread |
| 109 | + notify(); |
| 110 | + |
| 111 | + // and sleep |
| 112 | + Thread.sleep(1000); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
0 commit comments