• Post Reply Bookmark Topic Watch Topic
  • New Topic

How long will the InputStream of Socket be ready?

 
Sky Loi
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try to get the InputStream of Socket in server side as below:


and then check if the in is ready before read it:


When my system's load increases (about 200 concurrent request threads and created sockets increased), some of them take a quite long while to be ready, even I make sleep-to-check for 2 seconds. I get 2 - 6 sockets not-ready in each test.

My question is how to measure the time of the in.ready? What are related with that? Should I make the sleep longer?

Thank you.
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any reason you are checking if the stream is ready?
The method ready() is intended to indicate if the next read will wait for input, and it really doesn't guarantee that the read will not block if it returns true.
If you are trying to read from more than one input and only want to read when there's data available, I'd recommend nonblocking sockets.
Otherwise, I'd just call read() and let the call block until input is available.
 
Sky Loi
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Joe.

I am sorry I did not post all the details. Here will be:
1. use BufferedReader.ready() method to check if the stream is ready to read. Then, I use BufferedReader.readLine() method to read the stream to get the request string line.

2. The 200 concurrent test request threads hit my test server. My test server will generate specific number of sockets (200) to be connected.

3. The issue I found is, in some (4-6) sockets, the BufferedReader.ready() return false even they have waited for 2 seconds.

My guesses are:
1. if it is related with the performance of system loading. With the loading increase, it need longer time to make the BufferedReader.ready() return true

2. should I just change the time to wait for ready() return true, e.g. 3000ms or more.
 
Paul Clapham
Sheriff
Posts: 21889
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you repeatedly call the ready() method until it returns true, then you read from the stream? What's the point? Just start reading from the stream and the underlying socket code will automatically wait until there is data available. Works exactly the same, only better because you didn't have to write it.
 
Sky Loi
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Repost the details of code
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like others said there is no reason for doing that. Just call in.readLine() and it will block until there is something to read. You are doing this on 200 threads, that is a lot of overhead that isn't necessary.

Like Joe said, if you don't want it to wait for something, use nonblocking sockets.

Remove:


Your processor will thank you.
 
Sky Loi
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, many thanks to all.

I clean my codes and just use the following codes to block until the stream is ready and check if the request is NULL.



There are two different results:
1. In my local machine box (with better hardware), all 200 test threads are ok.
2. In the test machine box (with lower hardware), it has about 5-7 threads which the input is NULL after this code.

I checked the Java doc. The readLine() return NULL if the end of the stream has been reached. But why are there two different results?

 
Sky Loi
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I doubt the NULL return by in.readLine() is related with the system env or performance?
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe readline blocks until it has something to read or an IOException occurs, so your code is still redundant.
 
Sky Loi
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Rusty, my last codes just use the readLine() method to block for request input. And it has a test to check if the readLine() return NULL. Actually, there are about 6 threads which readLine() return NULL. I make sure the client thread input a valid string. Do you know what is the cause?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!