DOC PREVIEW
Penn CIT 591 - Producer Consumer

This preview shows page 1-2-3-4 out of 11 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Producer-ConsumerThe problemThe overall structureMethods in java.lang.ObjectThe put methodThe get methodThe Consumer classThe test classAnother Consumer classAnother test classThe EndJan 15, 2019Producer-ConsumerAn example of using ThreadsThe problemOne Thread, the producer, is “producing” information (creating objects), while another Thread, the consumer, is “consuming” (using) itWe want the consumer to use the objects in the same order as the producer creates themWe don’t want either Thread to be “loafing”—if there is work for it to do, the Thread should be doing itWe want to absolutely avoid busy loopsExample busy loop: while (!ready) { }(the other Thread will reset the ready variable)Busy loops will consume all available CPU cycles and seriously slow down everything on the computerThe overall structureimport java.util.Vector;public class CommandList { static Vector list = new Vector(); public static void put(String s) { // code on next slide } public static String get() { // code on slide after next }}Methods in java.lang.Objectvoid wait()Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.void wait(long timeout)Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed. void notify()Wakes up a single thread that is waiting on this object's monitor.void notifyAll()Wakes up all threads that are waiting on this object's monitor.The put method public static void put(String s) { synchronized (list) { list.add(s); list.notify(); } }The synchronized(list) block will take the list object as soon as it becomes available, and “lock” it so no other synchronized(list) can use it until this block is completedThere is nothing special about the list object; any object can be locked for synchronizationThe get method public static String get() { if (list.size() > 0) { synchronized (list) { return (String)list.remove(0); } } else { try { synchronized (list) { list.wait(); return get(); } } catch (InterruptedException e) { return "InterruptedException"; } } }}The Consumer classpublic class Consumer extends Thread { public void run() { while (true) { String s = CommandList.get(); System.out.println("Consuming " + s); } }}The test classpublic class CommandListTester { public static void main(String[] args) { Consumer consumer = new Consumer(); consumer.start(); CommandList.put("one"); CommandList.put("two"); sleep(2000); CommandList.put("three"); CommandList.put("four"); } private static void sleep(int ms) { try { Thread.sleep(ms); } catch (InterruptedException e) { } }}Output:Consuming oneConsuming twoConsuming threeConsuming fourAnother Consumer classimport java.util.Random;public class Consumer2 extends Thread { static Random rand = new Random(); public void run() { while (true) { String s = CommandList.get(); System.out.println("Consuming " + s); try {Thread.sleep(rand.nextInt(1000)); } catch (InterruptedException e) { } } }}Another test classimport java.util.Random;public class CommandListTester2 { static Random rand = new Random(100); public static void main(String[] args) { Consumer consumer = new Consumer(); consumer.start(); String[] words = { "one", "two", "three", "four", "five", "six" }; for (int i = 0; i < words.length; i++) { CommandList.put(words[i]); try { Thread.sleep(rand.nextInt(1000)); } catch (InterruptedException e) { } } }}The


View Full Document

Penn CIT 591 - Producer Consumer

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Download Producer Consumer
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Producer Consumer and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Producer Consumer 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?