code: private Thread[] enumerateThreads() {
          boolean fits = false;
          int inc = 50;
          Thread[] threads = null;
          int nrThreads = 0;
          while (!fits) {
              try {
                  threads = new Thread[Thread.activeCount()+inc];
                  nrThreads = Thread.enumerate(threads);
                  fits = true;
              } catch (ArrayIndexOutOfBoundsException exp) {
                  inc += 50;
              }
          }
          // Trim array
          Thread ret[] = new Thread[nrThreads];
          System.arraycopy(threads,0,ret,0,nrThreads);
          return ret;
      }
docs: Enumerate all active threads