title: Block until DataInputStream is available
body: <p>I am making an HTTP server as a project for a class that I am attenting ,for a week now I have stuck trying to find a solution .I have a DataInputStream and I need to wait for the client to send me the http request,there can pass hours before that happens since the connection remains  open.This is my code </p>
  
  <pre><code>    DataInputStream dis=new DataInputStream(socket.getInputStream());
      DataOutputStream dos =new DataOutputStream(socket.getOutputStream());
  
      while(!socket.isClosed()){
          try{
              /**wait until there are new data in to the stream,if the connection is no more alive then close it*/
              while(dis.available()==0){
                  if(alive==false){
                      socket.close();
                      break;
                  }
              }
  
              /*at this point the stream has new data ,or the alive attribute has been set to false */
              if(!socket.isClosed()){
                  /*parse the request text */
                  Request request=new Request(dis,this);
  
                  /*generate a response based on the request*/
                  Response response=new Response(request,this);
  
                  /*send the response back to the client*/
                  response.send(dos);
  
                  /*log the details of the communication*/
                  Logger.log(toString(request,response,socket));
  
                  /*if the request is bad formatted or it has its Connection header set to close , close the connection after sending the response*/
                  if(request.isBadRequest() || !"keep-alive".equalsIgnoreCase(request.getHeader("Connection"))){
                      close();
                  }
              }
          } catch (IOException e) {
              e.printStackTrace();
              break;
          }
      }
  </code></pre>
  
  <p>on the <code>while(dis.available()==0)</code> part I am waiting for the stream to have some data,but the problem is that if I have many connections my server starts to get very much cpu time slowing my computer down since it just hangs doing the loop again and again not giving the chance to the cpu to do good scheduling.If the dis.available() was a blocking command then everything would had been perfect.Any workaround?</p>