commit fef978b1725346ec6539a2f0232060d814f83d0c
parent d104d25283589db6f16beef6ebcb5acc7bddb02f
Author: William Lindholm <william_lindholm@outlook.com>
Date: Tue, 21 Feb 2023 15:18:19 +0100
Minor fixes, graceful shutdown
Diffstat:
4 files changed, 33 insertions(+), 8 deletions(-)
diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeMaker.java b/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeMaker.java
@@ -73,6 +73,16 @@ public class CoffeeMaker extends Thread {
/**
+ * Used to cancel the scheduled task.
+ * Subsequently closes thread.
+ */
+ public void cancel() {
+ createCoffee.cancel();
+ serveCoffee.cancel();
+ }
+
+
+ /**
* Constructor, requires variable coffeeQueue
* @param coffeeQueue The queue that workers use to interface with the coffeemaker.
*/
diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeQueue.java b/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeQueue.java
@@ -32,6 +32,7 @@ public class CoffeeQueue {
return coffeeQueue.remove();
}
+
/**
* Remove specific worker from coffeeQueue.
* @param w the worker to remove.
@@ -49,6 +50,7 @@ public class CoffeeQueue {
return coffeeQueue.size();
}
+
/**
* Used to determine if worker exists in queue.
* @param w the worker whose presence we are observing.
diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Main.java b/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Main.java
@@ -13,7 +13,6 @@ public class Main extends Thread{
Worker worker4 = new Worker("worker4", queue);
//start all threads. (start simulation).
-
worker1.start();
worker2.start();
worker3.start();
@@ -23,8 +22,14 @@ public class Main extends Thread{
//wait for 20 seconds
Thread.sleep(20000);
- //exit simulation
- System.exit(0);
+ //stop all threads. (exit simulation).
+ worker1.cancel();
+ worker2.cancel();
+ worker3.cancel();
+ worker4.cancel();
+ coffeeMachine.cancel();
+
+ System.out.println("======Closed Simulation=====");
}
}
diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Worker.java b/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Worker.java
@@ -26,7 +26,7 @@ public class Worker extends Thread {
energy--;
//if the worker has replenished energy, the worker should no longer be on break.
- if (energy >= 100 && onBreak == true) {
+ if (energy >= 100 && onBreak) {
System.out.println(name + " goes back to work with energy level " + energy);
onBreak = false;
}
@@ -39,8 +39,8 @@ public class Worker extends Thread {
*/
if (energy <= 0) {
System.out.println(name + " is going home with energy level " + energy);
- task.cancel();
- } else if (energy < 30 || onBreak == true) {
+ cancel();
+ } else if (energy < 30 || onBreak) {
onBreak = true;
System.out.println(name + " is taking a break with energy level " + energy);
Queue();
@@ -59,6 +59,14 @@ public class Worker extends Thread {
timer.scheduleAtFixedRate(task, T / timeScale, T / timeScale);
}
+ /**
+ * Used to cancel the scheduled task.
+ * Subsequently closes thread.
+ */
+ public void cancel() {
+ task.cancel();
+ }
+
/**
* Constructor
@@ -115,7 +123,7 @@ public class Worker extends Thread {
* Allows a worker to drink a cup of coffee
* @param coffee the coffee to drink
*/
- public void drink(Coffee coffee) {
+ public synchronized void drink(Coffee coffee) {
this.energy += coffee.getEnergy();
}
@@ -132,7 +140,7 @@ public class Worker extends Thread {
* Get the ammount of energy the worker currently has.
* @return energy as int.
*/
- public int getEnergy() {
+ public synchronized int getEnergy() {
return this.energy;
}