I have a thread busy-looping until my ServerSocket
object binds to a port before doing any read/write operation.
new Thread(() -> {
while (!server.isBound()) {
Thread.yield(); // hmm ... any better alternative????
}
while (!server.isClosed()) {
// do stuff regarding server and clients
}
}, "Server Connection Accept Thread").start();
I've used Thread.yield()
in the busy-loop to let other threads run (if there are any) while this thread loops, but the javadoc for Thread.yield()
says
It is rarely appropriate to use this method. It may be useful for debugging or testing purposes, where it may help to reproduce bugs due to race conditions.
Now, I don't have a race condition here, I just need to wait until the socket is bound and since there is no callback when the socket gets bound, I'm stuck with this approach. Any advice?