Good evening, guys!
I'm developing a multithreaded client / server system. Several clients connect, and when they send the string "Ocupada"
3 times, the code exits the while
, starts counting the time and ends the connection with it. When the client is connected again, if the sending 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 client only, since the count does not agree when more than one client sends "Ocupada"
.
I would like help with implementing the timer as a thread, so that the various clients can access and display the timing of each separately.
The program already counts separately the number of times that each client sent the string "Ocupada"
through the cont
variable. However, in the timer this does not happen. For a client the count is done perfectly, only the values do not agree when more than one client accesses.
Within while
, the else if
block is a game that I did (still think it was the best option) that guarantees that temporizador.stop()
and temporizador.difTime(String)
do not execute before temporizador.start()
, since at the beginning of the timer is stopValue = 1
and difTime = 1
and this.cont = 0
reset count if client 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);
}