Reputation: 515
I have a program which creates 4 Threads.
In each thread I create a tcp connection.
Each connection has its BufferedOutputStream and BufferedReader.
Each TCP connection is handled by separated Thread
The purpose of each thread is to process an array that I pass inside.
The problem is, that time spent on running each thread doesn't vary, according to amount of threads I create.
After a long time sniffing, I found out that the problem is in reading from InputStream, precisely in line
while((line=reader.readLine())!=null){do stuff}
When I comment this line of code, program run much faster, as it should be.
I agree that thread gets blocked while reading from stream and it takes let's say 1 sec, but if I increase number of threads, job must be done faster.
but in my case, in both 1 or 4 threads, I process same data, the same amount of time.
What am I doing wrong ? Please help me out.
Upvotes: 1
Views: 87
Reputation: 718926
It sounds like your application is network-bound. Basically, then network (or the server or client at the other end of the connection) cannot deliver data data enough to keep the threads busy.
It is unlikely that you can do anything in this application to significantly improve throughput. You might get some traction by making the other end go faster ... or getting a faster end-to-end network connection.
So it seems like, I have multiple threads, but all of them read from single BufferedReader, or something like that.
That contradicts what you said earlier. If you have 4 threads that are reading and writing 4 separate sockets, then they will NOT be sharing a single BufferedReader
. The blocked threads are likely to be blocked while waiting to read on different readers.
Upvotes: 1
Reputation: 237
FileInputStream does an I/O operation on every read and it synchronizes on all method calls to make it thread-safe. To reduce this overhead, read multiple bytes at once into a buffer array of bytes
FileInputStream f = new FileInputStream( name );
enterbyte[] barray = new byte[SIZE];
long checkSum = 0L;
int nRead;
while ( (nRead=f.read( barray, 0, SIZE )) != -1 )
for ( int i=0; i<nRead; i++ )
checkSum += barray[i];
Upvotes: 1
Reputation: 533530
Most likely you are using blocking IO but don't expect it to block.
I suggest you either use;
Upvotes: 2