Yogesh
Yogesh

Reputation: 725

java.io.BufferedReader.readLine() consuming 99% CPU Why?

In this code i am reading a value from stream. But readline() method is taking more CPU.Can you help me why is this so and give me suggession to minimize CPU usage.

    HttpURLConnection request = (HttpURLConnection) url.openConnection();
    request.setRequestMethod("POST");
    request.setRequestProperty("Content-Length", "0");
    request.setUseCaches(false);

    InputStreamReader in = new InputStreamReader((InputStream) request.getContent());
    BufferedReader buff = new BufferedReader(in);  

    while(line = buff.readLine() != null) {
   System.out.print("hello");
    }

Upvotes: 1

Views: 1226

Answers (2)

Buhb
Buhb

Reputation: 7149

I'm pretty sure the print statement is the reason for the high cpu load. What happens if you remove it?

Upvotes: 1

user207421
user207421

Reputation: 311028

You should be attaching the BufferedReader to the input stream of the request, but I find it hard to believe the statement in your title.

Upvotes: 1

Related Questions