title: Too many open threads error
body: <p>I'm implementing a multithreaded Wget with the URLs in the Open Directory Project (ODP). I get the following error: </p>
  
  <pre><code>java.io.FileNotFoundException: log (Too many open files)
  at java.io.FileOutputStream.openAppend(Native Method)
  at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:207)
  at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:131)
  at java.io.FileWriter.&lt;init&gt;(FileWriter.java:78)
  at ODP.run(ODP.java:103)
  </code></pre>
  
  <p>I've read that it's because it overpasses the limit of open file descriptors indicated in the system variable:  <strong>cat /proc/sys/fs/file-max</strong></p>
  
  <p>Is there a way to limit the number of threads that can be started at the same time? I thought that it did it automatically by the number of threads that could run the CPU.</p>
  
  <p>Thank you!</p>
  
  <p><strong>Edit:</strong> this is the main loop inside the main method:</p>
  
  <pre><code>        while (rs.next ()) {                
              // Process entry
              String rsc = rs.getString ("resource");
              String tpc = rs.getString("topic");             
              (new ODP(rsc, tpc, rs.getString("description"))).start();   
              BufferedWriter outLog = new BufferedWriter(new FileWriter("log", true));
      outLog.write(count + ": " + rsc + " | " + tpc  + "\n");
      outLog.close(); 
              ++count;                
          }
  </code></pre>
  
  <p>In the <strong>ODP.run()</strong> method I create a BufferedWritter:</p>
  
  <pre><code>        BufferedWriter out = new BufferedWriter(new FileWriter(cat.split("/")[1] + ".webtrec", true));
          out.write(webtrec);
          out.close();
  </code></pre>