title: Thread stops when creating TCP connection
body: <p>I am building an application that starts a Connection via a Thread but when i initialize the Connection the Application doesn't connect and stops (it continues to work but it does nothing).
  1 week ago it did work without any troubles, but when i updated Android Studio it started causing me this problem. </p>
  
  <p>This is the abstract class of the Connection:</p>
  
  <pre><code>public abstract class Connection implements Runnable{
  
  private Socket socket;
  private PrintWriter writer;
  private BufferedReader reader;
  private Semaphore semaphore;
  private ArrayList&lt;String&gt; messageQueue;
  private Thread connection;
  //private String state; // CONNECTED,RUNNING,STOPPED,RESTARTED,CLOSED
  
  Connection(InetAddress address, int port) {
      try {
          socket = new Socket(address,port);
          onConnectionEstablished(socket);
          writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
          reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
          semaphore = new Semaphore(1);
          messageQueue = new ArrayList&lt;&gt;();
      } catch (Exception e) {
          close();
      }
  }
  
  @Override
  public void run() {
      connection = Thread.currentThread();
      while(!connection.isInterrupted()){
          this.read();
      }
  }
  
  // Checks if connection is established
  boolean isConnectionEstablished(){
      return socket != null &amp;&amp; !socket.isClosed();
  }
  
  public abstract Socket onConnectionEstablished(Socket socket);
  
  // Restarts the connection
  public abstract void onConnectionClose();
  
  // Launched when message is received
  public abstract String onMessageReceived(String message);
  
  // Launched when message is posted
  public abstract String onMessagePosted(String message);
  
  // Launched when message is sent
  public abstract String onMessageSent(String message);
  
  // Post message
  public void postMessage(String message){
      messageQueue.add(message);
      sendMessage();
  }
  
  // Send Message
  private void sendMessage() {
      final String message = messageQueue.get(0);
      onMessagePosted(message);
      new Thread(new Runnable() {
          @Override
          public void run() {
              try {
                  semaphore.acquire();
                  System.out.println(Colors.color("[*] Sending: \'"+message+"\' [*]","blue"));
                  writer.print(message);
                  writer.flush();
                  messageQueue.remove(0);
                  semaphore.release();
              } catch (Exception e) {
                  close();
              }
          }
      }).start();
      onMessageSent(message);
  }
  
  // Read messages
  private void read(){
      try {
          String message = reader.readLine();
          if(message != null &amp;&amp; message.length() &gt; 0)
              onMessageReceived(message);
          else
              throw new Exception();
      } catch (Exception e) {
          close();
      }
  }
  
  // Close the connection
  private void close(){
      try{
          connection.interrupt();
          this.writer.close();
          this.reader.close();
          this.socket.close();
      }catch(Exception e){
          System.out.println(Colors.color("[X] Error while closing [X]","red"));
      }
      onConnectionClose();
  }
  
  // Restart the connection
  void restart(){
      new Thread(new Runnable() {
          @Override
          public void run() {
              try {
                  TimeUnit.SECONDS.sleep(5);
              } catch (InterruptedException e) {
                  System.out.println(Colors.color("[X] Error while restarting [X]","red"));
              }
              new Thread(new ConnectionInitializer()).start();
          }
      }).start();
  }
  
  }
  </code></pre>
  
  <p>While doing some debugging i found out that it apparently blocks when the socket is instantiated.</p>