고정 된 간격으로 UDP 패킷을 보내는 스레드가 있습니다. 잠시 후 나는 다른 스레드에서 interrupt()를 호출하고 있으며, 그 이후에는 완전히 보낸 스레드를 기다리고있다. 대부분의 경우, 송신기 스레드는 인터럽트를 수신 한 후에 마칩니다. 드문 경우지만, 보낸 사람 스레드는 그렇지 않습니다. 내 스레드 코드에서 실수를 발견하도록 도와 주시겠습니까? 나는 코드의 특정 부분에, 당신은 예외 : InterruptedException을 잡기 후 interruptedException.Restore에게 인터럽트를 삼키는 볼 수 있듯이스레드 인터럽트가 항상 작동하지 않는 경우
try {
DatagramSocket socket = null;
Timber.d("Initialize the sender...");
while (!Thread.currentThread().isInterrupted()) {
try {
Timber.d("Sending UDP broadcasts...");
socket = new DatagramSocket();
while (!Thread.currentThread().isInterrupted()) {
String s = "hello";
byte[] buffer = s.getBytes();
DatagramPacket packet = new DatagramPacket(
buffer, buffer.length,
mBroadcastAddress, PORT);
try {
if (BuildConfig.DEBUG)
Timber.d("[" + new DateTime().toLocalTime() + "] " +
"Send UDP packet");
socket.send(packet);
} catch (IOException ioe) {
Timber.d(ioe, "IOException");
}
Thread.sleep(TIMEOUT_SLEEP);
}
} catch (SocketException se) {
Timber.d(se, "Socket exception");
break;
} finally {
if (socket != null)
socket.close();
socket = null;
}
}
} catch (InterruptedException ie) {
Timber.d("The sender thread received interrupt request");
}
Timber.d("Finish the sender...");
확실히 인터럽트가 호출되고 있습니까? –