This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
My query is about readLine() and when it returns null.
I've got a chat client/server (from the good ole Head First guys) and in the server code it sets up the following runnable job that's loaded into a thread:
A message is sent to the server every time the "Send" button on the GUI (not shown) is clicked and ends up in the run() method above.
What is confounding me is that once the message has been sent, picked up in the run() method and printed out, the while loop loops back to the readLine() and as there isn't a message straight away won't it return null?
I know this isn't the case because if it were the run() method would complete and the thread would die. This doesn't happen because I can type in and send another message any time and it gets printed out so the thread must still be running.
So the question is why isn't readLine() returning null if there's nothing to read? and since it clearly isn't returnng null when will it return null
I know this must be simple but it's not clear to me and I've looked through the doco and it hasn't helped me so far.
An input stream read method 'blocks until input data is available, the end of the stream is detected, or an exception is thrown.' There is therefore a difference between no data being available and the end of the stream being reached. The latter will probably not happen until the socket is closed, but this is probably dependent on what the socket is connecting to.
Joined: Jul 14, 2008
Ahh! Thanks Joanne.
I knew it had to be something like that and now you mention it I think I remember reading that somewhere