title: OutOfMemoryError: unable to create new native thread
body: <p>I have two different A and B web applications hosted on tomcat. The B application is hosted on 4 physical systems with separate tomcat. And application A is hosted on 5th physical system with separate tomcat. Whenever login/logout event happens on application A, it needs to send this notification to all 4 B applications. Application A creates the one thread for each event, and this thread make the request to all 4 B applications using following code:</p>
  
  <pre><code>public class SendNotification implements Runnable {
    private String action;
    private String loginid;
    private Thread thread;
    StringBuilder sb  =     new StringBuilder();
  
    public static SendNotification getInstance() {
          return new SendNotification();
    }
    public void sendNotification(String action, String loginid){
          this.action = action;
          this.loginid = loginid;
          sb.append(loginid)
          .append("_")
          .append(action)
          .append(new Date().getTime());
          thread = new Thread(this, sb.toString());
          thread.start();
    }
  
    public void run(){
          String methodName =     "run";
          int   len = 4;
          for(int i=0;i&lt;len;i++){
                synchronized(this){
                      StringBuilder url = new StringBuilder()
                      .append(&lt;Server_B_i_IP:PORT&gt;)
                      .append("&amp;action=").append(action)
                      .append("&amp;loginid=").append(loginid);
                      URL urlObj                    =     null;
                      InputStream openStream  =     null;
                      try {
                            urlObj = new URL(url.toString());
                            openStream = urlObj.openStream();
                      } catch (Exception ex) {
                            ex.printStackTrace();
                      } finally {
                            try {
                                  if(openStream != null){
                                        openStream.close();
                                  }
                                  openStream  =     null;
                                  urlObj            =     null;
                                  url               =     null;
                            } catch (IOException e) {
                                  e.printStackTrace();
                            }
                      }
                }           
          }
          destroyObject();
    }
  
    private void destroyObject() {
          thread      =     null;
    }
  </code></pre>
  
  <p>}</p>
  
  <p>But after some time tomcat start throwing following exception:</p>
  
  <pre><code>Exception in thread "qe01lj_login_success1320054407929" java.lang.OutOfMemoryError: unable to create new native thread
                  at java.lang.Thread.start0(Native Method)
                  at java.lang.Thread.start(Thread.java:574)
                  at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:836)
                  at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1030)
                  at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1057)
                  at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1041)
                  at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:402)
                  at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:170)
                  at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:917)
                  at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
                  at java.net.URL.openStream(URL.java:1007)
                  at com.test. sendNotification.run(sendNotification.java:69)
                  at java.lang.Thread.run(Thread.java:595)
  </code></pre>
  
  <p>How can I resolve above issue?</p>
  
  <p>These applications are running HTTPS on Tomcat 5.5.17 with jdk1.5.0_13</p>