Doubt: Variable increment in Java

0

I have a jFrame (server) that by pressing its button does the following and sends the thread1 ...

    try {
        ss = new ServerSocket(9999);

        Hilo1 hilo = new Hilo1(ss,jLabel1,jLabel2,i);
        hilo.setDaemon(true);
        hilo.start();

        // System.out.println(i);
        //s = ss.accept();
        //JOptionPane.showMessageDialog(null, "Cliente conectado correctamente ");
    } catch (IOException ex) {
        Logger.getLogger(estacion.class.getName()).log(Level.SEVERE, null, ex);
    }

}  

Afterwards, in thread 1 I have a while that accepts client connections in this way and sends it to Hilo2, what I want with this is that the variable "i" increases through client connections ...

public void run(){  

    while(i<3 && i>=0){

        try {
            sock = ss.accept();
            i++;
            JOptionPane.showMessageDialog(null, "Cliente"+i+ "conectado correctamente ");
            System.out.println("cliente"+i+"conectado");

            if(i==1){
                j1.setText("ocupado");
            }

            if(i==2){
                j2.setText("ocupado");
            }

            Hilo2 hilo2 = new Hilo2(i,sock);
            hilo2.setDaemon(true);
            hilo2.start();

            //prueba.entero(i);
        } catch (IOException ex) {
            Logger.getLogger(Hilo1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    //
    JOptionPane.showMessageDialog(null, "No hay espacio");
}

In Hilo2 I want the variable "i" to decrease when the jFrame2 button is pressed, which simulates that the client left and there is a place available for another client to connect ...

Button exit from JFrame2 (client):

try {
        salida = new DataOutputStream(s.getOutputStream());
        salida.writeUTF("salir");
    } catch (IOException ex) {
        Logger.getLogger(Carro.class.getName()).log(Level.SEVERE, null, ex);
    }

Hilo2:

  public void run(){
      try {
          entrada = new DataInputStream(s.getInputStream());
          msg = entrada.readUTF();

          if(msg.equals("salir")){ 
              i--;
              JOptionPane.showMessageDialog(null, "un carro se fue "+i);
              prub = new Estacionamiento();
              prub.entero(i);
          }
      } catch (IOException ex) {
          Logger.getLogger(Hilo2.class.getName()).log(Level.SEVERE, null, ex);
      }
  }  

The problem is that it does not decrease the i every I give out, everything else works well. The objective of this is to simulate a parking lot with a sockets that when a car (client) arrives at the parking lot (server) the available places are filled, in this case 2, and when they leave there will be places again available and the parking lot accepts cars again.

    
asked by Jonu 01.05.2018 в 16:32
source

1 answer

0

What you need is for both threads to communicate with each other. You could pass the reference of Hilo1 when creating instances of Hilo2 , so that the latter will inform the first one that has ended.

For example, in Hilo2 :

    public Hilo2(int i, Socket sock, Hilo1 hilo1) {
        this.i = i;
        this.s = sock;
        this.hilo1 = hilo1;
    }

    public void run() {
        try {
            DataInputStream entrada = new DataInputStream(s.getInputStream());
            String msg = entrada.readUTF();

            if (msg.equals("salir")) {
                hilo1.liberarEstacionamiento();
                i--;
                JOptionPane.showMessageDialog(null, "un carro se fue " + i);
                prub = new Estacionamiento();
                prub.entero(i);
            }
        } catch (IOException ex) {
            Logger.getLogger(Hilo2.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

And in Hilo1 :

    public void run() {
        while (true) {
            while (i < 3 && i >= 0) {
                try {
                    Socket sock = ss.accept();
                    int c = i + 1;
                    JOptionPane.showMessageDialog(null, "Cliente" + c + "conectado correctamente ");
                    System.out.println("cliente" + c + "conectado");

                    if (c == 1) {
                        j1.setText("ocupado");
                    }

                    if (c == 2) {
                        j2.setText("ocupado");
                    }

                    Hilo2 hilo2 = new Hilo2(c, sock, this);
                    hilo2.setDaemon(true);
                    hilo2.start();

                    utilizarEstacionamiento();
                    //prueba.entero(i);
                } catch (IOException ex) {
                    Logger.getLogger(Hilo1.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public synchronized void utilizarEstacionamiento() {
        ++i;

        while (i >= 3) {
            JOptionPane.showMessageDialog(null, "No hay espacio");
            try {
                wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(Hilo1.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public synchronized void liberarEstacionamiento() {
        --i;
        notifyAll();
    }

The idea of the above is that to increase or decrease the variable i is done in a synchronized way obtaining the lock from the same thread .

If it is increased outside the admitted range of clients, then the thread that listens for more clients is waiting until more spaces are released.

When a space is released, all threads are notified waiting to verify their conditions.

    
answered by 02.05.2018 в 22:13