Advertisements
Offline
booboo0187
Posts: 1
Joined: Sun Jan 27, 2013 4:00 am
Location: Los Angeles
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 to cause these 2 problems? Thanks in advance.

- Brian