title: Create a thread to send and wait for response (websockets)
body: <p>Have a method to connect to socket and send the message and sleep for a sec to process the response.</p>
  
  <pre><code>import com.ning.http.client.AsyncHttpClient;
  import com.ning.http.client.websocket.WebSocket;
  import com.ning.http.client.websocket.WebSocketTextListener;
  import com.ning.http.client.websocket.WebSocketUpgradeHandler;
  import org.codehaus.jettison.json.JSONArray;
  import org.codehaus.jettison.json.JSONObject;
  import org.jboss.netty.handler.codec.http.HttpHeaders;
  import org.json.simple.parser.JSONParser;
  import org.json.simple.parser.ParseException;
  import org.slf4j.LoggerFactory;
  
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.concurrent.ExecutionException;
  
  
  public class SocketClient implements WebSocketTextListener {
  
      private static org.slf4j.Logger log = LoggerFactory.getLogger(SocketClient.class);
      private WebSocket websocket;
      private List&lt;String&gt; serverResponse = new ArrayList&lt;String&gt;();
      private List&lt;String&gt; logFromResponse = new ArrayList&lt;String&gt;();
  
      public List&lt;String&gt; getAllLogs(String receiverId){
          logFromResponse = new ArrayList&lt;String&gt;();
          JSONObject jObj = new JSONObject();
          try {
              jObj.put("deviceId", receiverId);
          }catch(Exception e){
              log.warn("JSON Exception: "+e.getLocalizedMessage(), e);
          }
          connectToSocket();
          if (websocket!=null)
              websocket.sendTextMessage(jObj.toString());
  
          responseFromServer();
  
          return logFromResponse;
      }
  
      public boolean clearForDevice(String receiverId){
          JSONObject jObj = new JSONObject();
          try {
              jObj.put("deviceId", receiverId);
          }catch(Exception e){
              log.warn("JSON Exception: "+e.getLocalizedMessage(), e);
          }
  
          connectToSocket();
          if (websocket!=null)
              websocket.sendTextMessage(jObj.toString());
  
          String res = responseFromServer();
          return (res!=null) &amp;&amp; res.contains("Success");
      }
  
      private void connectToSocket() {
          AsyncHttpClient c = new AsyncHttpClient();
  
          AsyncHttpClient.BoundRequestBuilder builder = c.prepareGet(createUri())
                  .addHeader(HttpHeaders.Names.CONNECTION, "Upgrade")
                  .addHeader(HttpHeaders.Names.UPGRADE, "WebSocket");
  
          websocket = builder.execute(new WebSocketUpgradeHandler.Builder()
                  .addWebSocketListener(this).build()).get(); 
      }
  
      @Override
      public void onOpen(WebSocket webSocket) {
          log.debug("Opening WebSocket");
      }
  
      @Override
      public void onClose(WebSocket webSocket) {
          log.debug("Closing WebSocket");
      }
  
      @Override
      public void onError(Throwable throwable) { }
  
      @Override
      public void onMessage(String message) {
          serverResponse.add(message);
      }
  
      @Override
      public void onFragment(String s, boolean b) {    }
  
      private String responseFromServer() {
          String response = null;
          sleep(100);
          if (!serverResponse.isEmpty()) {
              //format message and send response and add to logFromResponse
          }
          return response;
      }
  
      protected void sleep(long millis) {
          try {
              Thread.sleep(millis);
          } catch (InterruptedException e) {
              log.warn("Interrupted Exception: "+e.getLocalizedMessage(), e);
          }
      }
  }
  </code></pre>
  
  <p>but, I want to do it without the sleep() for 1 sec, Connecting to socket using AsyncHttpClient which is not Blocking io. I would like to create a thread which can send the message and wait for the response from the server? I did try to create a class by implementing runnable interface but no luck!
  Any suggestions how to create synchronous call to websockets or Blocking IO.</p>
  
  <p><strong>UPDATE:</strong> Using synchronized blocks to wait and then notify once the message is received! Followed @Antoniossss comment</p>