title: Java - URL Connection in Threads
body: <p>I currently have a project where different parameters are requested from an online CGI file, and each request is supposed to be processed in different threads. When I run my code by itself it works great, however it doesn't seem to connect when I put it in a thread. 
  My code is below:</p>
  
  <pre><code>public void run() {
      connect();
  }
  
  
  public synchronized void connect(){
      StringBuffer response = new StringBuffer("");
      try {
  
          String data = "year=" + year + "&amp;top=" + numNames + "number=";
          // Send data
          URL url = new URL("http://www.ssa.gov/cgi-bin/popularnames.cgi");
          URLConnection conn = url.openConnection();
          conn.setDoOutput(true);
          OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
          wr.write(data);
          wr.flush();
          // Get the response
          BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
          String line;
  
          while ((line = rd.readLine()) != null) {
              response.append(line);
          }
          wr.close();
          rd.close();
      } catch (Exception e) {
          System.out.println(e);
      }
  System.out.println(response);
          }
      }
  </code></pre>