Certain methods in Java will block until they can do something, like ServerSocket.accept() and InputStream.read(), but how it does this is not easy for me to find. The closest thing I can think of is a while() loop with a Thread.sleep() each time through, but the longer the sleep period, the less responsive the blocking, and the shorter the sleep, the more spinning that occurs.
I have two questions:
How do various standard functions, like the ones above, block? Native code? while() loops? Something else?
How should I implement methods that block?
The operations you've listed block because of the underlying platform (ie. native code).
You can implement a block using Java's Object.wait()
and Object.notify()
methods; wait()
will block the calling thread until another thread calls notify()
on the same lock.
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