Results 1 to 3 of 3
  1. #1
    booboo0187 is offline Member
    Join Date
    Jan 2013
    Location
    Los Angeles
    Posts
    2
    Rep Power
    0

    Default Multithreading question

    Hi all. I bought Osborne's "Teach Yourself Java" book about 5 weeks ago and I have run into an issue with one of the exercises and wanted to get some input from you guys. Here is the exercise:

    A set of ten mice continually enter and exit a box. Each mouse spends between 10 and 20 seconds out of the box, enters, spends between 5 and 8 seconds in the box, and exits. A maximum of four mice may be in the box at any time. If the box is full when another mouse wishes to enter, that animal must wait. Write a multithreaded program to simulate the behavior of this system. Establish a separate thread to manage the behavior of each mouse. Display the number of mice inside the box each time a mouse enters or exits.

    Here is my code:

    class Box {
    static int count = 0;

    synchronized int enter(String s) {
    try {
    while(count==4) {
    wait();
    }
    }
    catch(InterruptedException e) {
    e.printStackTrace();
    }
    int timein = (int)(Math.random()*3000+5000);
    System.out.println(s + ": enters, " + (timein/1000) + " secs. " + (++count) + " mice in.");
    return timein;
    }

    synchronized int exit(String s) {
    int timeout = (int)(Math.random()*10000+10000);
    System.out.println("Mouse exits. " + (--count) + " mice in.");
    notifyAll();
    return timeout;
    }
    }

    class Mouse extends Thread {
    String s;
    Box b = new Box();

    Mouse(String s) {
    this.s = s;
    }

    public void run() {
    while(true) {
    try {
    Thread.sleep(b.enter(s));
    Thread.sleep(b.exit(s));
    }
    catch(InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }

    class MiceInBox {
    public static void main(String args[]) {
    Mouse m[] = new Mouse[10];
    for(int i = 0; i < 10; i++) {
    new Mouse(args[i]).start();
    }
    }
    }


    Here is sample output from the program:

    java MiceInBox Harold Jason George Henry Mickey Bernie Milhouse Jack Jill Minnie
    Jason: enters, 6 secs. 2 mice in.
    Henry enters, 7 secs. 3 mice in.
    Harold: enters, 5 secs. 1 mice in.
    Bernie: enters, 7 secs. 4 mice in.
    Mouse exits. 3 mice in.
    Mouse exits. 2 mice in.
    Mouse exits. 1 mice in.
    Mouse exits. 0 mice in.
    Jason: enters, 5 secs. 1 mice in.
    Harold: enters, 6 secs. 2 mice in.
    Bernie: enters, 5 secs. 3 mice in.
    Mouse exits. 2 mice in.
    Henry: enters, 7 secs. 3 mice in.
    Mouse exits. 2 mice in.
    Mouse exits. 1 mice in.
    Mouse exits. 0 mice in.
    Bernie: enters, 5 secs. 1 mice in.
    Jason: enters, 6 secs. 2 mice in.
    Henry: enters, 5 secs. 3 mice in.
    Harold: enters, 5 secs. 4 mice in.
    Mouse exits. 3 mice in.


    Now, the program compiles and encounters no errors while running. It allows only 4 mice to be "in the box" at any given time. However, it never lets any mice but the first four in. And as you can see from the beginning of the sample output, it doesn't display the mice going in for the first time in the right order - "2 mice in, 3 mice in, 1 mice in, 4 mice in". Can anyone explain to me what my code is missing or what is happening behind the scenes that I am unaware of that would cause these 2 problems? Thanks in advance.

    - Brian

  2. #2
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Smile Re: Multithreading question

    Hi Brian,

    1. Answer to your first issue that it only allows first four mice to enter is:

    public Box b = new Box();
    make variable 'b' 'static' as there should be only one box available for all the 10 mice. Like below:
    public static Box b = new Box();

    2. Your second issue is that mice are not entering in sequence.
    For this you have to set priority of each thread you are generationg and use notify() method in place of notifyAll().


    3. Third issue which i noticed is that while exiting your are not checking wheather there is any mouse inside the box or not. If there is no mouse inside, you should wait. check below modified code in green.

    synchronized int exit(String s)
    {
    try
    {
    while (count <= 0)
    {
    wait();
    }
    }
    catch (InterruptedException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    int timeout = (int) (Math.random() * 3000 + 5000);
    System.out.println("Mouse exits in. " + (timeout / 1000) + " secs," + (--count) + " mice in.");
    notifyAll();
    return timeout;
    }

    4. Another mistake is you have swaped exit and enter timings. I mean the time you are using for enter should be for exit and vice-versa.

    I hope it will help you.

    Thanks
    Vijay Raghuvanshi

  3. #3
    booboo0187 is offline Member
    Join Date
    Jan 2013
    Location
    Los Angeles
    Posts
    2
    Rep Power
    0

    Default Re: Multithreading question

    Yes, this did help. Thank you! Now I have to figure out a method to form a priority queue for the waiting mouse threads so that each one gets a fair turn at calling the b.enter(s) method. I just learned about the Vector class and this may do the trick, but an array will probably suffice to do the job. What do you think?

Similar Threads

  1. multithreading
    By praveenbhushan1989 in forum Threads and Synchronization
    Replies: 8
    Last Post: 04-17-2011, 03:03 PM
  2. multithreading
    By praveenbhushan1989 in forum Threads and Synchronization
    Replies: 15
    Last Post: 03-01-2011, 01:17 AM
  3. Log 4j Multithreading
    By joe2010 in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-31-2010, 03:48 AM
  4. multithreading
    By shilpa.krishna in forum New To Java
    Replies: 2
    Last Post: 06-27-2008, 04:18 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
  •