2
I’ve tried using the methods wait()
with notify
and even the depreciated methods stop()
, and did not succeed.
I have three (3) threads which are:
Tankcontroller
Faucetfill
Faucetempty
What I’m trying to do is simply this:
a) Tankcontroller controls Faucetfill and Faucetempty threads.
b) Faucetfill has a water flow rate of 10 liters every 150ms.
c) Faucetempty has a water flow rate of 30 liters every 150ms.
How Tankcontroller Works?
a) The tank controller is responsible for opening and closing the taps.
b) When the tank reaches its maximum volume, the tap filler is closed and the tap emptying is opened.
c) When the tank is below 50% of its volume the filling tap shall be opened.
d) When the volume of water is below the tap, the tap shall be turned off.
What problem I’m having?
I cannot properly manipulate the Faucetfill and Faucetempty threads within the Tankcontroller thread to be able to open and close the tap in its particular condition.
Main java.
public class Main {
public static void main(String[] args) {
Tank t = new TankWater();
FaucetFill ff = new FaucetFill(t);
FaucetEmpty fe = new FaucetEmpty(t);
TankController tc = new TankController(t, ff, fe);
ff.start();
fe.start();
tc.start();
}
}
Tankcontroller.java
public class TankController extends Thread {
private final TankWater tank;
private final FaucetFill ff;
private final FaucetEmpty fe;
public TankController(Tank t, FaucetFill ff, FaucetEmpty fe) {
this.tank = t;
this.ff = ff;
this.fe = fe;
}
@Override
public void run() {
System.out.println("TANK-CONTROLLER RUNNING!");
// CÓDIGO DE CONTROLE DAS TORNEIRAS
}
}
Faucetfill.java
public class FaucetFill extends Thread {
private final TankWater tank;
private final int timer = 150;
private int litersWater = 10;
private boolean isStarted;
public FaucetFill(Tank t) {
this.tank = t;
}
public void setIsStarted(boolean state) {
this.isStarted = state;
}
@Override
public void run() {
while(this.isStarted) {
System.out.println("FAUCETFILL RUNNING");
Thread.currentThread().sleep(this.timer);
this.tank.setAmountWater(this.litersWater);
}
}
}
Faucetempty.java
public class FaucetEmpty extends Thread {
private final TankWater tank;
private final int timer = 150;
private int litersWater = 30;
private boolean isStarted;
public FaucetEmpty(Tank t) {
this.tank = t;
}
public void setIsStarted(boolean state) {
this.isStarted = state;
}
@Override
public void run() {
while(this.isStarted) {
System.out.println("FAUCETEMPTY RUNNING");
Thread.currentThread().sleep(this.timer);
this.tank.removeWater(this.litersWater);
}
}
}
Tankwater.java.
import java.math.BigDecimal;
public class TankWater {
private final int maximumCapacity = 2000;
private int amountWater = 0;
public int getAmountWater() {
return amountWater;
}
public void setAmountWater(int amountWater) {
if(this.getPercentageWater() != 100)
this.amountWater += amountWater;
}
public void removeWater(int quantity) {
if(this.getAmountWater() - quantity >= 0)
this.setAmountWater(this.getAmountWater() - quantity);
}
public double getPercentageWater() {
BigDecimal value = new BigDecimal(this.maximumCapacity);
BigDecimal rate = new BigDecimal(this.getAmountWater());
BigDecimal percentage = rate.divide(value).multiply(new BigDecimal("100"));
return percentage.doubleValue();
}
}
Thiago, you can as an alternative implement the Singleton standard in your main and your threads monitor the main class, it would be kind of an inverted control, I believe have better ways to circumvent, but it works in practice.
– Filipe L. Constante
TankWater
does not extendThread
, because you say that it is a Thread? From what I see it is accessed by Threads. Also note that yoursetAmountWater(int amountWater)
does not protect the tank from "overflowing" (even if the tank is empty and you pass as argument 3000 the tank will be with 3 thousand liters, well above the capacity and therefore in an inconsistent state); this problem will also occur if it is with some 1995 liters and theFaucetFill
add +10 liters, it goes to 2005 liters.– Douglas
@Douglas on the Tankwater is fixed, really it is not a Thread. The other questions I corrected and got the answer algorithm I gave below.
– user148754