| 1 | /*
|
---|
| 2 |
|
---|
| 3 | Juan Guillen Scholten.
|
---|
| 4 | Leiden University.
|
---|
| 5 |
|
---|
| 6 | An example of monitor use in Java.
|
---|
| 7 |
|
---|
| 8 | There are N drinkers drinking beer in a pub. After a while some of them want to
|
---|
| 9 | go to the toilet, unfortunately there is just one available. So only
|
---|
| 10 | one drinker can enjoy the toilet at the same time and the rest must wait.
|
---|
| 11 |
|
---|
| 12 | */
|
---|
| 13 |
|
---|
| 14 | //
|
---|
| 15 | // The monitor class.
|
---|
| 16 | //
|
---|
| 17 | class Toilet
|
---|
| 18 | {
|
---|
| 19 | private boolean occupied = false;
|
---|
| 20 |
|
---|
| 21 | Toilet () {}; // constructor
|
---|
| 22 |
|
---|
| 23 | //
|
---|
| 24 | // Try to enter the toilet. If occupied then
|
---|
| 25 | // drinker has to wait, else drinker can enter.
|
---|
| 26 | //
|
---|
| 27 | synchronized void try_to_enter ()
|
---|
| 28 | {
|
---|
| 29 | while (occupied)
|
---|
| 30 | {
|
---|
| 31 | try { wait(); } catch (InterruptedException e)
|
---|
| 32 | {
|
---|
| 33 | //
|
---|
| 34 | // In Java you always have to catch an exception
|
---|
| 35 | // when doing a wait.
|
---|
| 36 | //
|
---|
| 37 | System.out.println("interruptedException caught");
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
| 40 | occupied = true;
|
---|
| 41 | }// try_to_enter
|
---|
| 42 |
|
---|
| 43 | //
|
---|
| 44 | // Leave the toilet. Do a notify for if somebody else
|
---|
| 45 | // is waiting.
|
---|
| 46 | //
|
---|
| 47 | synchronized void leave_toilet ()
|
---|
| 48 | {
|
---|
| 49 | occupied = false;
|
---|
| 50 | notify(); // same as signal
|
---|
| 51 | }// leave_toilet
|
---|
| 52 | }// Toilet
|
---|
| 53 |
|
---|
| 54 | class Drinker extends Thread
|
---|
| 55 | {
|
---|
| 56 | int id;
|
---|
| 57 | private Toilet toilet;
|
---|
| 58 |
|
---|
| 59 | Drinker (int number, Toilet a)
|
---|
| 60 | {
|
---|
| 61 | super ("Drinker"+number);
|
---|
| 62 | this.id = number;
|
---|
| 63 | toilet = a;
|
---|
| 64 | }// constructor
|
---|
| 65 |
|
---|
| 66 | public void run ()
|
---|
| 67 | {
|
---|
| 68 | int times;
|
---|
| 69 | int index = 0;
|
---|
| 70 | int sleeptime;
|
---|
| 71 |
|
---|
| 72 | times = (int) (Math.random () * 20); // number of times
|
---|
| 73 | // going to the toilet
|
---|
| 74 | while ( index < times )
|
---|
| 75 | {
|
---|
| 76 | //
|
---|
| 77 | // Drinking beer.
|
---|
| 78 | //
|
---|
| 79 | sleeptime = (int) (Math.random () * 2000); // 2000 = 2 sec.
|
---|
| 80 | try {sleep (sleeptime);} catch (Exception e) {}
|
---|
| 81 |
|
---|
| 82 | //
|
---|
| 83 | // Trying to enter the toilet.
|
---|
| 84 | //
|
---|
| 85 | System.out.println("Drinker "+ id +" wants to go to the toilet.");
|
---|
| 86 | toilet.try_to_enter();
|
---|
| 87 |
|
---|
| 88 | //
|
---|
| 89 | // Using the toilet.
|
---|
| 90 | //
|
---|
| 91 | System.out.println("Drinker "+ id +" enters the toilet.");
|
---|
| 92 | sleeptime = (int) (Math.random () * 2000);
|
---|
| 93 | try {sleep (sleeptime);} catch (Exception e) {}
|
---|
| 94 |
|
---|
| 95 | //
|
---|
| 96 | // Leaving the toilet.
|
---|
| 97 | //
|
---|
| 98 | System.out.println("Drinker "+ id +" leaves the toilet and says: NEXT!");
|
---|
| 99 | System.out.println(" ");
|
---|
| 100 | toilet.leave_toilet();
|
---|
| 101 | index++;
|
---|
| 102 |
|
---|
| 103 | }
|
---|
| 104 | System.out.println("Drinker "+ id + " goes home.");
|
---|
| 105 | System.out.println(" ");
|
---|
| 106 | }// run
|
---|
| 107 | }// Drinker
|
---|
| 108 |
|
---|
| 109 | public class Beer_Drinkers
|
---|
| 110 | {
|
---|
| 111 | public static void main (String args[])
|
---|
| 112 | {
|
---|
| 113 | Toilet toilet;
|
---|
| 114 | int N = 4; // number of drinkers
|
---|
| 115 | int i;
|
---|
| 116 | System.out.println("Amount of drinkers : "+ N + ".");
|
---|
| 117 | System.out.println(" ");
|
---|
| 118 | toilet = new Toilet();
|
---|
| 119 | Drinker [] drinker = new Drinker [N];
|
---|
| 120 |
|
---|
| 121 | //
|
---|
| 122 | // Creating the drinkers.
|
---|
| 123 | //
|
---|
| 124 | for (i = 0; i < N; i++)
|
---|
| 125 | {
|
---|
| 126 | drinker[i] = new Drinker (i, toilet);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | //
|
---|
| 130 | // Starting the drinkers.
|
---|
| 131 | //
|
---|
| 132 | for (i = 0; i < N; i++)
|
---|
| 133 | {
|
---|
| 134 | drinker[i].start();
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | }// main
|
---|
| 138 | }// Beer_Drinkers
|
---|
| 139 |
|
---|
| 140 |
|
---|