Download presentation
Presentation is loading. Please wait.
Published byBarnard Wheeler Modified over 8 years ago
1
Producer/Consumer CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L
2
Outline USC CSCI 201L2/9 ▪P▪Producer/Consumer ▪P▪Program
3
Producer/Consumer Overview ▪The producer/consumer problem is a famous problem for concurrent programming ▪Assume you have a soda machine with a number of delivery people (producers) and a number of people wanting to buy sodas (consumers) ›Each producer can insert as many sodas as he has, up to the capacity of the soda machine ›Each consumer can purchase as many sodas as he wants, up to the current number in the soda machine ▪If there are not enough sodas for a consumer to buy, he needs to wait until there are ▪If there is not enough room for the producer to insert the number of sodas he has, he needs to wait until there is ▪This problem can produce a situation called deadlock ▪Download the ProducerConsumerWithMonitors.java from the course web site and execute it ›What line(s) of code produce the potential deadlock? USC CSCI 201L3/9 Producer/Consumer
4
Producer/Consumer Example with Monitors 1 import java.util.LinkedList; 2 import java.util.concurrent.ExecutorService; 3 import java.util.concurrent.Executors; 4 5 public class ProducerConsumerWithMonitors { 6 private static Buffer buffer = new Buffer(); 7 8 public static void main(String [] args) { 9 ExecutorService executor = Executors.newFixedThreadPool(2); 10 executor.execute(new ProducerTask()); 11 executor.execute(new ConsumerTask()); 12 executor.shutdown(); 13 } 14 15 private static class ProducerTask implements Runnable { 16 public void run() { 17 try { 18 int i = 1; 19 while (true) { 20 System.out.println("Producer writes: " + i); 21 buffer.write(i++); 22 Thread.sleep((int)(Math.random() * 1000)); 23 } 24 } catch (InterruptedException ie) { 25 System.out.println("Producer IE: " + ie.getMessage()); 26 } 27 } 28 } 29 30 private static class ConsumerTask implements Runnable { 31 public void run() { 32 try { 33 while (true) { 34 System.out.println("\t\tConsumer reads: " + buffer.read()); 35 Thread.sleep((int)(Math.random() * 1000)); 36 } 37 } catch (InterruptedException ie) { 38 System.out.println("Consumer IE: " + ie.getMessage()); 39 } 40 } 41 } USC CSCI 201L4/9 Producer/Consumer 42 private static class Buffer { 43 private static final int CAPACITY = 1; 44 private LinkedList queue = new LinkedList (); 45 private static Object notEmpty = new Object(); 46 private static Object notFull = new Object(); 47 48 public void write(int value) { 49 synchronized(notFull) { 50 synchronized(notEmpty) { 51 try { 52 while (queue.size() == CAPACITY) { 53 System.out.println("Wait for notFull condition " + value); 54 notFull.wait(); 55 } 56 queue.offer(value); 57 notEmpty.notify(); 58 } catch (InterruptedException ie) { 59 System.out.println("Buffer.write IE: " + ie.getMessage()); 60 } 61 } 62 } 63 } 64 65 public int read() { 66 int value = 0; 67 synchronized(notFull) { 68 synchronized(notEmpty) { 69 try { 70 while (queue.isEmpty()) { 71 System.out.println("\t\tWait for notEmpty condition"); 72 notEmpty.wait(); 73 } 74 value = queue.remove(); 75 notFull.notify(); 76 } catch (InterruptedException ie) { 77 System.out.println("Buffer.read IE: " + ie.getMessage()); 78 } 79 } 80 } 81 return value; 82 } 83 } // ends class Buffer 84 } // ends class ProducerConsumerWithMonitors
5
Monitors, Locks, Semaphores ▪Monitors are an older technology that allow us to synchronize specific blocks of code or methods ›There is the potential of deadlock that can be caused when multiple blocks of code need to be synchronized on multiple objects (as in the previous example) ▪Locks provide the underlying mechanism for monitors, but we can also explicitly define our own locks ›This allows us to create multiple conditions on a single lock, providing the ability to avoid the deadlock problem allowed by monitors ▪Semaphores allow more than one thread to obtain permits to access a specific block of code ›This would be equivalent to having more than one of the same lock available on a block of code ›We will talk about semaphores in a future lecture USC CSCI 201L5/9 Producer/Consumer
6
Producer/Consumer Example with Locks/Conditions 1 import java.util.LinkedList; 2 import java.util.concurrent.ExecutorService; 3 import java.util.concurrent.Executors; 4 import java.util.concurrent.locks.Condition; 5 import java.util.concurrent.locks.Lock; 6 import java.util.concurrent.locks.ReentrantLock; 7 8 public class ProducerConsumerWithLocks { 9 private static Buffer buffer = new Buffer(); 10 11 public static void main(String [] args) { 12 ExecutorService executor = Executors.newFixedThreadPool(2); 13 executor.execute(new ProducerTask()); 14 executor.execute(new ConsumerTask()); 15 executor.shutdown(); 16 } 17 18 private static class ProducerTask implements Runnable { 19 public void run() { 20 try { 21 int i = 1; 22 while (true) { 23 System.out.println("Producer tries to write: " + i); 24 buffer.write(i++); 25 Thread.sleep((int)(Math.random() * 1000)); 26 } 27 } catch (InterruptedException ie) { 28 System.out.println("Producer IE: " + ie.getMessage()); 29 } 30 } 31 } 32 33 private static class ConsumerTask implements Runnable { 34 public void run() { 35 try { 36 while (true) { 37 System.out.println("\t\tConsumer reads: " + buffer.read()); 38 Thread.sleep((int)(Math.random() * 1000)); 39 } 40 } catch (InterruptedException ie) { 41 System.out.println("Consumer IE: " + ie.getMessage()); 42 } 43 } 44 } USC CSCI 201L6/9 Producer/Consumer 45 private static class Buffer { 46 private static final int CAPACITY = 1; 47 private LinkedList queue = new LinkedList (); 48 private static Lock lock = new ReentrantLock(); 49 private static Condition notEmpty = lock.newCondition(); 50 private static Condition notFull = lock.newCondition(); 51 52 public void write(int value) { 53 lock.lock(); 54 try { 55 while (queue.size() == CAPACITY) { 56 System.out.println("Wait for notFull condition " + value); 57 notFull.await(); 58 } 59 queue.offer(value); 60 notEmpty.signal(); 61 } catch (InterruptedException ie) { 62 System.out.println("Buffer.write IE: " + ie.getMessage()); 63 } finally { 64 lock.unlock(); 65 } 66 } 67 68 public int read() { 69 int value = 0; 70 lock.lock(); 71 try { 72 while (queue.isEmpty()) { 73 System.out.println("\t\tWait for notEmpty condition"); 74 notEmpty.await(); 75 } 76 value = queue.remove(); 77 notFull.signal(); 78 } catch (InterruptedException ie) { 79 System.out.println("Buffer.read IE: " + ie.getMessage()); 80 } finally { 81 lock.unlock(); 82 return value; 83 } 84 } 85 } // ends class Buffer 86 } // ends class ProducerConsumerWithLocks
7
Producer/Consumer Output with Locks/Conditions USC CSCI 201L7/9 Producer/Consumer
8
Outline USC CSCI 201L8/9 ▪M▪Monitors ▪P▪Program
9
Program ▪Modify the Producer/Consumer program with locks so that a Producer will never write two lines in a row. Also provide an output line when a Producer or Consumer is notified or signaled. USC CSCI 201L9/9 Program
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.