I have a thread that blocks on a UDP packet, and I need to be able to tell it to forget about that packet and do something else, all before the receive timeout happens. Is there any way to do this?
Use a DatagramChannel to read your UDP packets, and interrupt the reading thread. As per the documentation of Thread.interrupt (and DatagramChannel), the read operation will then throw a ClosedByInterruptException.
JB has posted one part of the solution. But if in case you are not using NIO channels, the solution AFAIK here would be to close the socket in consideration and handle it likewise in your runnable/callable. I did something similar a while back with TCP sockets, in case if you are interested. The feasibility of the solution again depends on whether closing the socket would be acceptable in your case or not. In that case, going with the NIO solution would make much more sense.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With