2
Good night, you guys!
I am developing a multithread client/server system. Multiple clients connect and when they send the string "Ocupada"
3 times, the code leaves the while
, starts the time count and ends the connection with it. When the customer is turned on again, if the shipment is not "Busy", it shows the time that was turned off. Code below:
while (entrada.hasNextLine() && this.cont < 3) {
saida.println("Situação?");
String sit = entrada.nextLine();
System.out.println(sit);//recebe situação
if ("Ocupada".equals(sit)) {
this.cont++;
} else if(temporizador.getStop() == 0 && temporizador.getDifTime() == 0 ) { // faz o stop e exibe tempo
temporizador.stop();
temporizador.difTempo(this.nomeDispositivo);
}else{
this.cont = 0;
}
System.out.println(this.cont);
}
//inicia a contagem aqui e só exibe quando o temporizador.stop() for chamado ( dentro do while)
if (temporizador.getStart() == 0){
temporizador.start();
System.out.println("Start no tempo!");
}
The timer class is as follows:
public class temporizador {
private static long startValue = 0;
private static long stopValue = 1;
private static long difTime = 1;
public static void start() {
startValue = System.currentTimeMillis();
stopValue = 0;
difTime = 0;
}
public static void stop() {
stopValue = System.currentTimeMillis();
difTime = stopValue - startValue;
}
public static void difTempo(String nome) throws SQLException {
String format = String.format("%02d:%02d:%02d", difTime / 3600000, (difTime / 60000) % 60, (difTime / 1000) % 60);
System.out.println(nome + " levou " + format);
startValue = 0;
stopValue = 1;
difTime = 1;
}
public static long getStart(){
return startValue;
}
public static long getStop(){
return stopValue;
}
public static long getDifTime(){
return difTime;
}
}
It is working perfectly but for one customer only, since the count does not go according when more than one customer sends "Ocupada"
.
I would like a help to implement the timer as a thread, so that the various customers access and display the time of each separately.
The program already counts separately the number of times each client sent the string "Ocupada"
by means of the variable cont
. However, on the timer this does not happen. For a customer the count is done perfectly, only that the values do not go according when more than one customer accesses.
Within the while
, the block else if
is a gambiarra I did (I still think it was the best option) that ensures that the temporizador.stop()
and temporizador.difTime(String)
do not execute before a temporizador.start()
, since at the beginning of the timer is stopValue = 1
and difTime = 1
and the this.cont = 0
Zera the count in case the customer sends something other than "Ocupada"
.
Temporizador temp = temporizadores.get(this.nomeDispositivo);
System.out.println(temp);
while (entrada.hasNextLine() && this.cont < 3) {
saida.println("Situação?");
String sit = entrada.nextLine();
System.out.println(sit); // Mostra a situação
if ("Ocupada".equals(sit)) {
this.cont++;
} else if (temp != null) { // Para o temporizador e exibe o tempo.
System.out.println(temp.measureTimeElapsed().getReport());
temp = null;
temporizadores.put(this.nomeDispositivo, null);
} else {
this.cont = 0;
}
System.out.println(this.cont);// Contagem das vezes que esteve ocupada
String sql2 = "UPDATE vagas SET situacao = (?) WHERE nomeID = (?)";
try (PreparedStatement stmt = conecta.conn.prepareStatement(sql2)) {
stmt.setString(1, sit);
stmt.setString(2, this.nomeDispositivo);
stmt.executeUpdate();
}
}
//------------------------------------------------------------------------------
// Inicia a contagem aqui e só exibe quando o measureTimeElapsed() for chamado (dentro do while).
if (temp == null) {
temp = new Temporizador(this.nomeDispositivo);
temporizadores.put(this.nomeDispositivo, temp);
System.out.println("Start no tempo!");
System.out.println(temporizadores);
}
He must account for the receipt of "Busy" globally (i.e., three times independent of who sent it), or it must account separately for each client (i.e., each client has its own independent server-side processing)?
– Victor Stafusa
It already counts separately, the cont (which counts the times the customer sends "Busy") is done separately for each customer. On the timer that does not happen this, for a customer the count is done perfectly, only that the values do not go out according when more than one customer accesses.
– Leone Azevedo
I don’t understand the part
else if(temporizador.getStop() == 0 && temporizador.getDifTime() == 0 )
, what is the purpose of this? If the received string is different fromOcupada
, what is he trying to do? And theelse { this.cont = 0; }
? Under what circumstances should such structures be used?– Victor Stafusa
This was a "Gambiarra" I did (I still think it was the best option), in it if Else ensures that the timer.stop() and timer.Diftime() do not run before a.start() timer, since at the beginning of the timer is stopValue = 1 and difTime =1. and this.cont=0 Zera count in case the customer sends something other than "Busy".
– Leone Azevedo