Ho il seguente blocco di codice:Uscire da un thread
public void startListening() throws Exception {
serverSocket = new DatagramSocket(port);
new Thread() {
@Override
public void run() {
System.out.print("Started Listening");
byte[] receiveData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
while (!stopFlag) {
try {
serverSocket.receive(receivePacket);
String message = new String(receivePacket.getData());
System.out.println("RECEIVED: " + message);
} catch (Exception ex) {
System.out.print("Execption :" + ex.getMessage());
}
}
}
}.start();
}
public void stopListening() {
this.stopFlag = true;
}
Supponiamo che ho impostato stopFlag su true. serverSocket.receive(receivePacket);
attenderà fino a quando non riceverà un pacchetto. Cosa devo fare se voglio che il thread esca non appena stopFlag è impostato su true.
L'ho dimenticato completamente :). Spero che tu prenda la gratitudine che modificherò la mia risposta per i futuri lettori. –
@mistalee ha funzionato ... grazie –