Nonostante la variabile aggiornato sia volatile, quando il thread ritorna dal join la variabile risulta ancora false !! Ho messo dei commenti sulla situazione dove si verifica il problemacodice:public SWListaOperatori(ClienteDB cliente, FormListaClientiServer frame) { this.frameClienti = frame; this.cliente = cliente; } /** * Scarica tutti gli operatori , e ritorna una lista con tutti i nomi utente * * @return ArrayList conentente i nomi utente degli operatori * @throws Exception */ @Override protected ArrayList<Object> doInBackground() throws Exception { ResultSet rs = Database.getInstance().viewTableContent(GestoreDatabase.Tabella.OPERATORE); ArrayList<Object> list = new ArrayList<>(); while (rs.next()) { list.add(rs.getObject(1)); } rs.getStatement().close(); return list; } /** * Permette di effettuare il cambio di operatore attraverso l'interfaccia * grafica */ @Override protected void done() { ArrayList<Object> list = null; try { list = this.get(); } catch (InterruptedException | ExecutionException ex) { Logger.getLogger(SWListaOperatori.class.getName()). log(Level.SEVERE, null, ex); } if (list != null && !list.isEmpty()) { final Object choice = JOptionPane.showInputDialog(null, "Seleziona Operatore", "Assegna Cliente: " + cliente.getId_cliente() + " " + cliente.getNome() + " " + cliente.getCognome(), JOptionPane.QUESTION_MESSAGE, null, list.toArray(), 0); if (choice != null) { SWInviaAppuntamento swIA = new SWInviaAppuntamento((String) choice); swIA.execute(); } } else { JOptionPane.showMessageDialog(null, "Non ci sono altri operatori a cui assegnare"); } } private class SWInviaAppuntamento extends SwingWorker<Boolean, Void> { private final String userOperatore; private volatile boolean aggiornato; public SWInviaAppuntamento(String choice) { this.userOperatore = choice; aggiornato = false; } @Override protected Boolean doInBackground() throws Exception { if (this.userOperatore != null) { Operatore operatoreAttuale = Database.getInstance().getOperatoreClienteDB(cliente); if (!operatoreAttuale.getLogin().getUsername().equalsIgnoreCase(this.userOperatore)) { boolean appuntamentoDisponibile = !Database.getInstance().verificaDisponibilitaAppuntamento(userOperatore, cliente.getDataAppuntamento(), cliente.getOraAppuntamento()); if (!appuntamentoDisponibile) { Thread thread = new Thread(new Runnable() { @Override public void run() { int scelta = JOptionPane.showConfirmDialog(frameClienti, "L'operatore a cui e' stato assegnato l'appuntamento\n" + "non e' disponibile per la data e l'ora indicata in quello " + "\nche gli si vuole assegnare" + "\nE' possibile comunque assegnarlo," + "\ncambiando però prima la data dell'appuntamento\n Vuoi Procedere?"); if (scelta == JOptionPane.OK_OPTION) { final FormSchedaCliente schedaCliente = new FormSchedaCliente(frameClienti, frameClienti.getModelTable(), cliente); try { final Operatore oldOperator = Database.getInstance().getOperatoreClienteDB(cliente); Database.getInstance().updateField(10, 1, Integer.toString(cliente.getId_cliente()), (String) userOperatore, GestoreDatabase.Tabella.CLIENTE); schedaCliente.setup(); schedaCliente.disableAppuntamentoOKButton(); schedaCliente.disableAppuntamentoKOButton(); schedaCliente.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); schedaCliente.addWindowListener(new WindowAdapter() { @Override public void windowClosed(WindowEvent evt) { aggiornato=true;//la aggiorno quando si chiude la finestra synchronized (schedaCliente) { schedaCliente.notify();//e notifico la chiusura al thread } } @Override public void windowClosing(WindowEvent evt) { try { aggiornato=false; JOptionPane.showMessageDialog(schedaCliente, "Assegnazione Annullata"); Database.getInstance().updateField(10, 1, Integer.toString(cliente.getId_cliente()), oldOperator.getLogin().getUsername(), GestoreDatabase.Tabella.CLIENTE); synchronized (schedaCliente) { schedaCliente.notify();//il thread si risveglia e termina } } catch (SQLException ex) { Logger.getLogger(SWListaOperatori.class.getName()).log(Level.SEVERE, null, ex); } } }); schedaCliente.setRiprogrammaActionListener(new RiprogrammaAppuntamentoListener(schedaCliente)); schedaCliente.setAppuntamentoKOActionListener(new AppuntamentoKOListener(schedaCliente)); schedaCliente.setAppuntamentoOKActionListener(new AppuntamentoOkListener(schedaCliente)); schedaCliente.setVisible(true); synchronized (schedaCliente) { while (schedaCliente.isVisible()) { schedaCliente.wait(); } } } catch (ParseException | InterruptedException | SQLException | IOException ex) { Logger.getLogger(SWListaOperatori.class.getName()).log(Level.SEVERE, null, ex); } } } }); thread.start(); thread.join();//lui ritorna dal join , ma la variabile vale ancora false System.out.println("fine join"); } else if (appuntamentoDisponibile) { aggiornato = Database.getInstance().updateField(10, 1, Integer.toString(cliente.getId_cliente()), (String) this.userOperatore, GestoreDatabase.Tabella.CLIENTE); InetAddress addr = ServerListener.getAddressByUsername(this.userOperatore); if (addr != null) { InformazioniAlClient assegnaAppuntamento = new InformazioniAlClient(cliente, addr, Operazioni.AGGIORNA_APPUNTAMENTO); assegnaAppuntamento.inviaInfoAppuntamento(); } } if (aggiornato) { InetAddress addrOldOp = ServerListener.getAddressByUsername(operatoreAttuale.getLogin().getUsername()); //Comunico al vecchio operatore che l'appuntamento è stato assegnato ad altri if (addrOldOp != null) { InformazioniAlClient infoToOldOp = new InformazioniAlClient(cliente, addrOldOp, Operazioni.ASSEGNATO_ALTRO); infoToOldOp.inviaInfoAppuntamento(); } } }else{ aggiornato=false; } }else{ aggiornato=false; } return aggiornato; } @Override protected void done() { try { boolean val = this.get(); if (val) { JOptionPane.showMessageDialog(frameClienti, "Cliente Assegnato a " + this.userOperatore, "Assegnazione avvenuta", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(frameClienti, "Cliente non assegnato! Errore!", "Assegnazione non avvenuta", JOptionPane.ERROR_MESSAGE); } } catch (InterruptedException | ExecutionException ex) { Logger.getLogger(SWListaOperatori.class.getName()).log(Level.SEVERE, null, ex); } } }

Rispondi quotando



