Created
October 9, 2017 23:13
-
-
Save benwaffle/da83d9052ca28dbc2a6247a717787416 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/Assignment2/Exercise.java b/Assignment2/Exercise.java | |
index e962ea0..b4bd4fb 100755 | |
--- a/Assignment2/Exercise.java | |
+++ b/Assignment2/Exercise.java | |
@@ -78,16 +78,7 @@ public class Exercise { | |
for(WeightPlateSize wps : WeightPlateSize.values()) { | |
for(int i = 0 ; i < weights.get(wps); i++) { | |
try { | |
- switch (wps) { | |
- case SMALL_3KG: | |
- WeightPlateSize.wpPermissions.get(WeightPlateSize.SMALL_3KG).acquire(); | |
- break; | |
- case MEDIUM_5KG: | |
- WeightPlateSize.wpPermissions.get(WeightPlateSize.MEDIUM_5KG).acquire(); | |
- break; | |
- case LARGE_10KG: | |
- WeightPlateSize.wpPermissions.get(WeightPlateSize.LARGE_10KG).acquire(); | |
- } | |
+ wps.sem.acquire(); | |
noOfWeightPlates.put(wps, noOfWeightPlates.get(wps)-1); | |
} catch (InterruptedException e) { e.printStackTrace(); } | |
} | |
@@ -138,16 +129,7 @@ public class Exercise { | |
for(WeightPlateSize wps : WeightPlateSize.values()) { | |
for(int i = 0 ; i < weights.get(wps); i++) { | |
- switch (wps) { | |
- case SMALL_3KG: | |
- WeightPlateSize.wpPermissions.get(WeightPlateSize.SMALL_3KG).release(); | |
- break; | |
- case MEDIUM_5KG: | |
- WeightPlateSize.wpPermissions.get(WeightPlateSize.MEDIUM_5KG).release(); | |
- break; | |
- case LARGE_10KG: | |
- WeightPlateSize.wpPermissions.get(WeightPlateSize.LARGE_10KG).release(); | |
- } | |
+ wps.sem.release(); | |
noOfWeightPlates.put(wps, noOfWeightPlates.get(wps)+1); | |
} | |
} | |
diff --git a/Assignment2/Gym.java b/Assignment2/Gym.java | |
index e701b0d..bca7892 100755 | |
--- a/Assignment2/Gym.java | |
+++ b/Assignment2/Gym.java | |
@@ -32,9 +32,8 @@ public class Gym implements Runnable { | |
this.noOfWeightPlates = new HashMap<WeightPlateSize, Integer>(); | |
Gym.wpMutex.acquire(); | |
- this.noOfWeightPlates.put(WeightPlateSize.SMALL_3KG, WeightPlateSize.INIT_NUM_SMALL_PLATES); | |
- this.noOfWeightPlates.put(WeightPlateSize.MEDIUM_5KG, WeightPlateSize.INIT_NUM_MEDIUM_PLATES); | |
- this.noOfWeightPlates.put(WeightPlateSize.LARGE_10KG, WeightPlateSize.INIT_NUM_LARGE_PLATES); | |
+ for (WeightPlateSize wps : WeightPlateSize.values()) | |
+ this.noOfWeightPlates.put(wps, wps.numPlates); | |
Gym.wpMutex.release(); | |
} | |
diff --git a/Assignment2/WeightPlateSize.java b/Assignment2/WeightPlateSize.java | |
index d08ee3e..e0c9bf4 100755 | |
--- a/Assignment2/WeightPlateSize.java | |
+++ b/Assignment2/WeightPlateSize.java | |
@@ -1,27 +1,21 @@ | |
package Assignment2; | |
-import java.util.HashMap; | |
-import java.util.Map; | |
import java.util.concurrent.Semaphore; | |
/** | |
* Contains the declaration given above and possibly methods for generating a random weight plate size. | |
*/ | |
public enum WeightPlateSize { | |
- SMALL_3KG, | |
- MEDIUM_5KG, | |
- LARGE_10KG; | |
+ SMALL_3KG(110), | |
+ MEDIUM_5KG(90), | |
+ LARGE_10KG(75); | |
- public static final int INIT_NUM_SMALL_PLATES = 110; | |
- public static final int INIT_NUM_MEDIUM_PLATES = 90; | |
- public static final int INIT_NUM_LARGE_PLATES = 75; | |
+ public Semaphore sem; | |
+ public int numPlates; | |
- public static Map<WeightPlateSize, Semaphore> wpPermissions = new HashMap<WeightPlateSize, Semaphore>(); | |
- | |
- static { | |
- WeightPlateSize.wpPermissions.put(SMALL_3KG, new Semaphore(INIT_NUM_SMALL_PLATES)); | |
- WeightPlateSize.wpPermissions.put(MEDIUM_5KG, new Semaphore(INIT_NUM_MEDIUM_PLATES)); | |
- WeightPlateSize.wpPermissions.put(LARGE_10KG, new Semaphore(INIT_NUM_LARGE_PLATES)); | |
+ WeightPlateSize(int numPlates) { | |
+ this.numPlates = numPlates; | |
+ this.sem = new Semaphore(numPlates); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment