AnimalThread.java 919 Bytes
Newer Older
Michele Fiori's avatar
Michele Fiori committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
package veterinarian;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Random;

public class AnimalThread extends Thread {
    private WaitingRoom wr;
    private String animal;
    private Random r;
    private int id;

    public AnimalThread(WaitingRoom wr, String animal, Random r, int id) {
        this.wr = wr;
        this.animal = animal;
        this.r = r;
        this.id = id;
    }

    public void run() {
        System.out.println(animal + " " + id + " tries to enter");
        try {
            wr.EnterRoom(animal, id);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        int seconds = r.nextInt(10) + 1;
        try {
            Thread.sleep(seconds * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        wr.ExitRoom(animal, id);
    }
}