permaculture playing cards*
The moose likes Java in General and the fly likes didnt get this line of code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "didnt get this line of code?" Watch "didnt get this line of code?" New topic
Author

didnt get this line of code?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 688

this is the my server's code where i m trying to read data which client is sending me
and this is a thread which keep listening to the client .
but one thing i didnt understand is this statement


i know this a while loop and my thread will be in this loop forever


what exactly i want to learn is ...... suppose this thread listens to the client's data and the client sends the data and this code will receive it fine ....that means that the try block is completed successfully now my question is why the loop keep looping again and again why didnt it exit because the while condition is clearlly saying that exit when there is no data to read ....

how i have found this thing
suppose i have send a msg to server 1st time it is ok this thread will receive it but when i send it again it will still receive it that means my thread moves back and forth between
running and runnable state and keep running because of this while loop
but if i put a


The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 307
    
    2

I'll try

There's actually 2 things going on in that statement but they are often combined.

If you look up readline it returns a string until the end of file then it returns a null.

So msg = read.readline is reading the next line from the input stream. When it gets to the end it will return null.

When you put an assignment in quotes like (a=b) the value of that expression is the value that gets assigned so we can rewrite your while statement like


I like the first way better because duplicated code tends to get out of sync. Otherwise they are equivalent.

Joe

It's not what your program can do, it's what your users do with the program.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 688

Joe Areeda wrote:I'll try

There's actually 2 things going on in that statement but they are often combined.

If you look up readline it returns a string until the end of file then it returns a null.

So msg = read.readline is reading the next line from the input stream. When it gets to the end it will return null.

When you put an assignment in quotes like (a=b) the value of that expression is the value that gets assigned so we can rewrite your while statement like


I like the first way better because duplicated code tends to get out of sync. Otherwise they are equivalent.

Joe

ya , but my question is lets imaging that this thread (above code is a thread) listens the data and gets the complete data from the client ...fine
so that means try block had been completed ...right ?
if yes ....then if client send another data why this thread reads that data again and again because the try block is completed successfully so logically this thread should be terminated ...right ?
but why this is not happening here ...why it gets looping and looping i means it keep on listening by staying back & forth between running and runnable state ..why the hell it does not terminate
there and there after reading all the lines of data the client is sending to the server ?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
naved momin wrote: ya , but my question is lets imaging that this thread (above code is a thread) listens the data and gets the complete data from the client ...fine
so that means try block had been completed ...right ?

No. The try block will not be complete until the while loop terminates. The while loop will not terminate until the read.readLine() returns null and read.readLine() will not return null until the client closes the connection. If the client is not sending any data but has not closed the connection then read.readLine() will just block until data is sent or the connection is closed.


Joanne
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 688

Joanne Neal wrote:
naved momin wrote: ya , but my question is lets imaging that this thread (above code is a thread) listens the data and gets the complete data from the client ...fine
so that means try block had been completed ...right ?

No. The try block will not be complete until the while loop terminates. The while loop will not terminate until the read.readLine() returns null and read.readLine() will not return null until the client closes the connection. If the client is not sending any data but has not closed the connection then read.readLine() will just block until data is sent or the connection is closed.

thanks mate ....u make my day !

so i assume that while we use this kind of while loop for reading a file and when we reached to the end of the file null is returned ..right ?
but in networking this is not the same way
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
naved momin wrote:so i assume that while we use this kind of while loop for reading a file and when we reached to the end of the file null is returned ..right ?
but in networking this is not the same way

Correct. The javadoc for the reader usually explains what will cause null to be returned
 
 
subject: didnt get this line of code?
 
Similar Threads
threads concurrency problem
Need some advice
While loop problem
Seems like I can't read from a scoket?
why thread is in running or runnable state why it is not terminated ?