27
27
* A FruitShop contains three FruitBowl instances and controls access to them.
28
28
*/
29
29
public class FruitShop {
30
-
30
+
31
31
/**
32
32
* The FruitBowl instances stored in the class.
33
33
*/
34
34
private FruitBowl [] bowls = {
35
- new FruitBowl (),
36
- new FruitBowl (),
37
- new FruitBowl ()
35
+ new FruitBowl (),
36
+ new FruitBowl (),
37
+ new FruitBowl ()
38
38
};
39
39
40
40
/**
41
41
* Access flags for each of the FruitBowl instances.
42
42
*/
43
43
private boolean [] available = {
44
- true ,
45
- true ,
46
- true
44
+ true ,
45
+ true ,
46
+ true
47
47
};
48
48
49
49
/**
50
50
* The Semaphore that controls access to the class resources.
51
51
*/
52
52
private Semaphore semaphore ;
53
-
53
+
54
54
/**
55
- * FruitShop constructor
55
+ * FruitShop constructor.
56
56
*/
57
57
public FruitShop () {
58
58
for (int i = 0 ; i < 100 ; i ++) {
59
59
bowls [0 ].put (new Fruit (Fruit .FruitType .APPLE ));
60
60
bowls [1 ].put (new Fruit (Fruit .FruitType .ORANGE ));
61
61
bowls [2 ].put (new Fruit (Fruit .FruitType .LEMON ));
62
62
}
63
-
63
+
64
64
semaphore = new Semaphore (3 );
65
65
}
66
66
67
67
/**
68
- *
68
+ * Returns the amount of fruits left in shop.
69
+ *
69
70
* @return The amount of Fruit left in the shop.
70
71
*/
71
72
public synchronized int countFruit () {
72
73
return bowls [0 ].countFruit () + bowls [1 ].countFruit () + bowls [2 ].countFruit ();
73
74
}
74
-
75
+
75
76
/**
76
- * Method called by Customer to get a FruitBowl from the shop. This method
77
- * will try to acquire the Semaphore before returning the first available
78
- * FruitBowl.
79
- */
77
+ * Method called by Customer to get a FruitBowl from the shop. This method will try to acquire the
78
+ * Semaphore before returning the first available FruitBowl.
79
+ */
80
80
public synchronized FruitBowl takeBowl () {
81
-
81
+
82
82
FruitBowl bowl = null ;
83
-
83
+
84
84
try {
85
85
semaphore .acquire ();
86
-
86
+
87
87
if (available [0 ]) {
88
88
bowl = bowls [0 ];
89
89
available [0 ] = false ;
@@ -94,28 +94,27 @@ public synchronized FruitBowl takeBowl() {
94
94
bowl = bowls [2 ];
95
95
available [2 ] = false ;
96
96
}
97
-
97
+
98
98
} catch (InterruptedException e ) {
99
99
e .printStackTrace ();
100
100
} finally {
101
101
semaphore .release ();
102
102
}
103
103
return bowl ;
104
104
}
105
-
105
+
106
106
/**
107
- * Method called by a Customer instance to return a FruitBowl to the shop.
108
- * This method releases the Semaphore, making the FruitBowl available to
109
- * another Customer.
110
- */
107
+ * Method called by a Customer instance to return a FruitBowl to the shop. This method releases
108
+ * the Semaphore, making the FruitBowl available to another Customer.
109
+ */
111
110
public synchronized void returnBowl (FruitBowl bowl ) {
112
111
if (bowl == bowls [0 ]) {
113
112
available [0 ] = true ;
114
113
} else if (bowl == bowls [1 ]) {
115
114
available [1 ] = true ;
116
115
} else if (bowl == bowls [2 ]) {
117
- available [2 ] = true ;
116
+ available [2 ] = true ;
118
117
}
119
118
}
120
-
119
+
121
120
}
0 commit comments