For some time I've been studying through Streams and Sockets in Java. What everyone says is that read() blocks (especially in sockets where the net is far slower than our computer). I am trying to imagine how this works. I mean does it look like :
(PSEUDOCODE)
read() {
while (true) {
if (there is at least one byte to read) {
return this byte;
}
}
}
Or it has to do with other low level stuff? I assume Streams in Java are implemented in C/C++. My questions are, first how read() works and second does our thread get any kind of "sleep" when on read? I mean a while(true) is a waste for CPU cycles and I am wondering if the thread that blocks on read() gets notified by another thread "Hey your bytes are ready!" Thanks in advance.