Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By JosAH

Thread: Producer-Consumer Project

  1. #1
    future is offline Member
    Join Date
    Feb 2016
    Posts
    2
    Rep Power
    0

    Default Producer-Consumer Project

    Hello everyone, I'm new to java and now I really need your help to finish this project about Producer-Consumer problem. This one is kind of weird and I totally have no idea what it is talking about.

    Programming Task

    In the classic producer-consumer problem, a producer thread inserts items into a buffer, and a consumer thread consumes items from the buffer simultaneously. In this project, you are required to use Java Threads to solve an N- producer-M-consumer problem by generating N producer threads and M consumer threads to simultaneously access the buffer.

    In specific, your program should satisfy the following requirements:

    (1) Your program should accept 4 input parameters, namely “the number of producer threads”, “the number of consumer threads”, “the size of the buffer”, and “the number of items to be produced”. For example, the following command starts your program with 4 producer threads, 5 consumer threads, a buffer of 10 items, and generate 1000 items in total.
    $ ./producer-consumer 4 5 10 1000

    (2) Similar to the producer-consumer problem, a buffer is maintained in your program and shared among all producers and consumers. Each item in buffer is an integer.

    (3) Each producer thread generates a random integer each time, inserts it into the buffer, increments a counter, num_produced, which is shared by all producers to track how many items being created, and records the time and the item being produced in an event log. The event log should record the events in the order of timestamps.

    (4) Each consumer thread consumes the items of the buffer in the First-In-First-Out (FIFO) order, meaning that if item A is inserted into the buffer earlier than B, item A should be consumed earlier than B, increments a counter, num_consumed, which is shared by all consumers to track how many blocks are consumed, and record the time and the item being consumed in an event log. The event log should record the events in the order of timestamps.

    (5) Use either semaphore or mutex lock (spinlock) to synchronize threads working on the shared resources in your program.

    (6) When the specified number of items are produced and consumed, your program should exit and generate the following output: (i) print the total number of produced items, (ii) print the total number of consumed items, and (iii) output the even log files, named “producer-event.log” and “consumder-event.log”. Each record of the log file should be formatted as the following:
    <Timestamp (in nanoseconds)> <Thread type (“Producer” or “Consumer”)> <Thread ID> <Buffer Entry Index> <Item>.

    Basically, each record logs when and which thread produced an item into or consumed an item from an entry in the shared buffer.


    PLEASE HELP ME!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Producer-Consumer Project

    The buffer is the shared resource, so your producers and consumers should synchronize on that buffer. Producers should wait while the buffer is full, while consumers should wait while the buffer is empty; both should notifyAll() after they have done their deed ...

    kind regards,

    Jos

    ps. I closed your other (identical) thread.
    SurfMan likes this.
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Producer-Consumer Project

    Quote Originally Posted by JosAH View Post
    The buffer is the shared resource, so your producers and consumers should synchronize on that buffer. Producers should wait while the buffer is full, while consumers should wait while the buffer is empty; both should notifyAll() after they have done their deed ...
    @OP: This is a very useful reply. Read it carefully.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Producer-Consumer Project

    It sounds like the buffer is an abstract concept. Thus you might consider an implementation of one of the Queue class' to act as your buffer. And I would also recommend you break it up into parts. First write the producer (or consumer code) and test it. Then put it together later making adjustments as needed. And although this isn't required, you might want to consider having your producers generate "fake" tasks. These would be basically just sleeping for some random range of seconds or milliseconds. It allows things to slow down a bit so you can better see what is happening. And use lots of print statements to monitor execution.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Consumer/Producer with random numbers
    By devo22 in forum Threads and Synchronization
    Replies: 1
    Last Post: 11-13-2011, 06:57 PM
  2. producer/consumer problem
    By concaf in forum Threads and Synchronization
    Replies: 1
    Last Post: 09-20-2011, 02:07 PM
  3. Producer-Consumer Problem
    By kendel in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-04-2011, 01:09 PM
  4. Producer Consumer Synchronization Problem
    By rushhour in forum Threads and Synchronization
    Replies: 3
    Last Post: 11-23-2010, 07:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •