wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes While loop problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "While loop problem" Watch "While loop problem" New topic
Author

While loop problem

Jeremy Lee
Greenhorn

Joined: Jan 26, 2011
Posts: 5
Hi all,

Greenhorn here, would appreciate your kind assistance very much.

This code was from "Java head first 2nd edition, chapter 15, simple chat client".

The book creates the following sub class of Runnable:



(reader is an instance of BufferedReader used here to receive Strings from a server)

It is then placed in a thread:



A server is set up separately. It sends Strings to clients running the above code whenever it receives code from one of its clients (group chat program).

I've been trying to figure out why the JVM doesn't exit the while loop and subsequently kill the thread when clients are not sending Strings, tried out the following:

1. Checked if reader.readLine() returned null when the server was not sending Strings.

>>> Result: reader.readLine() never seemed to return null.

2. However, line 6 wasn't executing continuously. So i added the following just before line 6:

>>> Result: while loop ran only when the client received a string from the server.

3. Tested while loops and read up abit, thinking that it may not be the while loop though still can't figure out why the thread isn't killed yet line 6 (line 84 below) doesn't get executed continuously. Much appreciate any assistance.

The complete code is as follows:





Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

reader.readLine() will return if there is no more data, and it knows no more data will come. The latter part is important in networking; the only way to ensure that no data will come is to close the connection. After all, on an open connection any data can still be sent.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jeremy Lee
Greenhorn

Joined: Jan 26, 2011
Posts: 5
Rob Spoor wrote:reader.readLine() will return if there is no more data, and it knows no more data will come. The latter part is important in networking; the only way to ensure that no data will come is to close the connection. After all, on an open connection any data can still be sent.


Mr Spoor, thanks for your reply.

Does this mean that the JVM will pause at the readLine() method until either data comes in or the connection is closed?

Thanks all
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

It's even a bit worse. It doesn't pause until data comes in - it pauses until a line break comes in or the stream ends, whichever comes first.
Jeremy Lee
Greenhorn

Joined: Jan 26, 2011
Posts: 5
K got it, thanks again!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
And welcome to the Ranch
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: While loop problem
 
Similar Threads
Best way for login authentication
Seems like I can't read from a scoket?
Usability features
I need help improving this chat server and chat client!
How do you formally disconect a client from a server?