Producer-Consumer - PowerPoint PPT Presentation

About This Presentation
Title:

Producer-Consumer

Description:

Busy loops will consume all available CPU cycles and seriously slow down ... System.out.println('Consuming ' s); The test class. public class CommandListTester ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 12
Provided by: davidlm3
Category:

less

Transcript and Presenter's Notes

Title: Producer-Consumer


1
Producer-Consumer
  • An example of using Threads

2
The 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 dont want either Thread to be loafingif
    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 computer

3
The overall structure
  • import java.util.Vectorpublic 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

4
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.

5
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 synchronization

6
The get method
  • public static String get() if
    (list.size() gt 0) synchronized
    (list) return
    (String)list.remove(0)
    else try
    synchronized (list)
    list.wait() return get()
    catch
    (InterruptedException e) return
    "InterruptedException"

7
The Consumer class
  • public class Consumer extends Thread public
    void run() while (true)
    String s CommandList.get()
    System.out.println("Consuming " s)

8
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 four

9
Another Consumer class
  • import java.util.Randompublic 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)

10
Another test class
  • import java.util.Randompublic 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 lt
    words.length i)
    CommandList.put(wordsi) try
    Thread.sleep(rand.nextInt(1000))
    catch (InterruptedException
    e)

11
The End
Write a Comment
User Comments (0)
About PowerShow.com