Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By SurfMan

Thread: Locking and Synchronization program in Java

  1. #1
    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Default Locking and Synchronization program in Java

    I have this task of Creating a Locking and Synchronization program in Java, it can only be two files as stated; BufferQueue.java and ProducerConsumer.java.

    Create a class BufferQueue in a file called BufferQueue.java. The class should contain at least two methods - an enter() method takes a String as an argument and places it in the buffer, and a remove() method returns a String from the buffer and removes it. You should use wait() and notify() calls within synchronized enter()/remove() methods for the implementation.

    The next step is to create a driver program in a class called ProducerConsumer, stored in a file called ProducerConsumer.java. You should create two threads in this program, and you can copy your existing source code from your previous practicals (dropbox.com/s/g4k2eemkmm1mme8/DriverATM.zip). The producer thread should print out messages as it adds items to the BufferQueue, and the consumer thread should print out messages as it removes items from the BufferQueue. The actual printing should take place in the producer or consumer threads, and not in the BufferQueue itself.

    Put sleeping code into the producer to limit its speed of operation, and observe how the output varies depending on the amount of time sleeping and the size of the BufferQueue.

    Your output does not have to match any specific format, but for a buffer of size 5 it could look something like this:
    Producer: creating string 1
    Producer: creating string 2
    Producer: creating string 3
    Consumer: reading string 1
    Consumer: reading string 2
    Producer: creating string 4
    Producer: creating string 5
    Consumer: reading string 3
    Consumer: reading string 4

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Locking and Synchronization program in Java

    You seem to have forgotten to ask a question.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Locking and Synchronization program in Java

    If you enter the email address of your professor here, someone will create your assignment for you and mail it directly to your professor. That saves you even the hassle of doing anything yourself...

  4. #4
    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Default Re: Locking and Synchronization program in Java

    Im not asking for it to be fully coded as that would be plagiarizing, sorry for not being clear, new to these forums? Im not sure where to start or what methods I should use, and how wait() and notify() work in synchronized?
    Last edited by Javabeginner82; 05-03-2013 at 04:53 AM.

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,095
    Rep Power
    4

    Default Re: Locking and Synchronization program in Java

    I think it looks like a classic Producer/Consumer pattern: Producer ---> BufferedQueue ---> Consumer. Each Producer and Consumer are separate threads and work on the BufferedQueue simultaneously.

    Central point is the BufferedQueue where the Producer keeps adding items and the Consumer removes the item. A problem arises when the Consumers removes the items faster than the Producer creates them. That's where the wait() notify() come in. If the queue is empty and the Consumer wants an item, he is out of luck: he should wait() until an item comes available (See what I did there? :)). When the Producer adds an item it should notify() (doh!) any waiting Consumers that an item has become available. To see this happen let the Producer add 1 item per second and the Consumer get 2 items per second.

    So you should first create a BufferedQueue without worrying about Threads and synchronized. Just make the class so it works for the Producer, then the Consumer. After that, let the Producer and Consumer work on the queue at the same time and you will get errors/exceptions about the empty queue. This is when you start playing around with wait() and notify().

    I hope you can get started with this now.
    Last edited by SurfMan; 05-03-2013 at 03:21 PM. Reason: Typo
    Javabeginner82 likes this.

  6. #6
    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Default Re: Locking and Synchronization program in Java

    Quote Originally Posted by SurfMan View Post
    (See what I did there? :))
    Yes, yes I do, thanks for actually being helpful

Similar Threads

  1. synchronization in java threads
    By naurinfarooq in forum New To Java
    Replies: 4
    Last Post: 07-23-2012, 08:33 AM
  2. need help in locking or synchronization
    By samanyu in forum New To Java
    Replies: 24
    Last Post: 07-06-2011, 07:19 AM
  3. java RMI and synchronization
    By guest_user in forum New To Java
    Replies: 15
    Last Post: 07-05-2011, 01:28 AM
  4. One row help with Locking Java to MAC Adress
    By Darkbound in forum New To Java
    Replies: 1
    Last Post: 04-28-2011, 08:42 PM
  5. Locking window in java
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 07-31-2007, 08:21 AM

Tags for this Thread

Posting Permissions

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