How to create parallelism in a specific multithreading?

Asked

Viewed 73 times

0

My scenario is this::

Simulate a water tank associated with two taps: one used to fill the tank and another used to empty the tank.

Rules:

a) The total capacity of the tank is 2000 liters and each "ball" represents 10 liters.

b) The tap that fills the tank has a flow rate of 10 liters every 150 ms (milliseconds).

c) The tap that empties the tank has a flow rate of 30 liters every 150 ms (milliseconds).

d) The tank controller is responsible for opening and closing the taps. e) When the tank reaches its maximum volume, the tap filler is closed and the tap emptying is opened.

f) When the tank is below 50% of its volume the filling tap shall be opened.

g) When the volume of water is below the tap, the tap shall be turned off.

My problem:

How do I make the filler faucet (faucetA) and tap emptying (faucetB) have a parallelism according to my business rule?

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {

    public static void main(String[] args) {
        TankController tc = new TankController();
        tc.start();
    }

}

import java.math.BigDecimal;

public class Tank {
    private final int maximumCapacity = 2000;
    private int volumeOfWater;
    private boolean empty;
    private BigDecimal percentageOfWater;

    public int getVolumeOfWater() {
        return volumeOfWater;
    }

    public void setVolumeOfWater(int volumeOfWater) {
        if (this.exceededMaximumVolume(volumeOfWater))
            this.volumeOfWater = 2000;
        else
            this.volumeOfWater += volumeOfWater;
    }

    private boolean exceededMaximumVolume(int volumeOfWater) {
        return (this.getVolumeOfWater() + volumeOfWater > 2000) ? true : false;
    }

    public double getPercentageOfWater() {
        BigDecimal value = new BigDecimal(this.maximumCapacity);
        BigDecimal rate  = new BigDecimal(this.getVolumeOfWater());
        BigDecimal percentage = rate.divide(value).multiply(new BigDecimal("100"));

        return percentage.doubleValue();
    }

}


public class Faucet extends Thread {
    private String name;
    private int waterFlow;

    public Faucet(String name, int waterFlow) {
        this.name = name;
        this.waterFlow = waterFlow;
    }

    public int getWaterFlow() {
        return this.waterFlow;
    }

    @Override
    public void run() {
        switch(this.name) {
            case "faucetA":
                System.out.println("TORNEIRA DE ENCHER ABERTA.. ENCHENDO O TANQUE!");
            case "faucetB":
                System.out.println("TORNEIRA DE ENCHER FECHADA..");
                System.out.println("TORNEIRA DE VAZÃO ABERTA.. ESVAZIANDO O TANQUE!");
        }
    }

}

import java.math.BigDecimal;

public class TankController extends Thread {
    private Tank tank = new Tank();
    private final Faucet faucetA = new Faucet("faucetA", 10);
    private final Faucet faucetB = new Faucet("faucetB", 30);

    @Override
    public void run() {     
        this.faucetA.start();
        this.faucetB.start();

        while(true) {
            try {
                System.out.println(this.tank.getPercentageOfWater() + "%");

                if(this.tank.getPercentageOfWater() < 100) {
                    this.faucetA.sleep(150);
                    this.tank.setVolumeOfWater(this.faucetA.getWaterFlow());
                } else if(this.tank.getPercentageOfWater() == 100) {
                    System.out.println("TANQUE NA CAPACIDADE MÁXIMA..\n");
                    this.faucetB.sleep(150);
                    this.tank.setVolumeOfWater(this.tank.getVolumeOfWater() - this.faucetB.getWaterFlow());
                } else if (this.tank.getPercentageOfWater() < 50) {
                    System.out.println("TANQUE ABAIXO DA METADE DA CAPACIDADE..\n");
                    this.faucetA.sleep(150);
                    this.tank.setVolumeOfWater(this.faucetA.getWaterFlow());
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}

1 answer

-1

In the run method of the Facet class failed to break the switch case.

switch(this.name) {
        case "faucetA":
            System.out.println("TORNEIRA DE ENCHER ABERTA.. ENCHENDO O TANQUE!");
            break;
        case "faucetB":
            System.out.println("TORNEIRA DE ENCHER FECHADA..");
            System.out.println("TORNEIRA DE VAZÃO ABERTA.. ESVAZIANDO O TANQUE!");
 }

You also have to put a stop, if you will not give stackoverflow on while(true)

  • thanks for the answer. My question was specifically Multithread and parallelism. According to the Business Rules cited above, how do I correctly handle the taps (faucets)? I feel that my Tankcontroller class Run method is incorrect handling these threads. If answer I will point to your question.

  • could explain point g) ? How so smaller than the drain tap? When the tank has less than 30 lt?

  • when the tank is at < 50% the drain tap will be shut down and the fill tap will be opened.

  • Why did you use Bigdecimal? Here the numbers do not go beyond 2000 and do not require the use of this class.

  • I used to generate the percentage of liters in the tank.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.