private Set<Thread> collectCurrentThreads() {
   Thread[] activeThreadsT = new Thread[Thread.activeCount()];
   Thread.enumerate(activeThreadsT);
   Set<Thread> res = new HashSet<Thread>();
   for (Thread t : activeThreadsT) res.add(t);
   return res;
 }
 private boolean hasRetrievers() {
   Thread[] threads = new Thread[Thread.activeCount()];
   int numThreads = Thread.enumerate(threads);
   for (int i = 0; i < numThreads; i++) {
     if (threads[i].getName().startsWith(RUNNING_THREAD_NAME_PREFIX)) return true;
   }
   return false;
 }
 private static void listThreads() {
   int ac = Thread.activeCount();
   if (ac > 0) {
     Thread[] tlist = new Thread[ac];
     Thread.enumerate(tlist);
     for (Thread t : tlist) {
       System.out.println("Thread [" + t + "] => {" + t.getClass().getName() + "}");
     }
   }
 }
  @Override
  public String format(LogRecord record) {

    int count = Thread.activeCount();

    Thread th[] = new Thread[count];
    // returns the number of threads put into the array
    Thread.enumerate(th);

    String thrName = null;

    // prints active threads
    for (int i = 0; i < count; i++) {

      if (th[i] != null && th[i].getId() == record.getThreadID()) {

        thrName = th[i].getName();
        break;
      }
    }

    if (thrName == null) {
      thrName = String.format("id:%d", record.getThreadID());
    }

    StringBuilder sb = new StringBuilder();

    sb.append(new Date(record.getMillis()))
        .append(" ")
        .append(record.getLevel().getLocalizedName())
        .append(" ")
        .append("[")
        .append(record.getSourceClassName())
        .append("]")
        .append("{")
        .append(thrName)
        .append("}: ")
        .append(formatMessage(record))
        .append(LINE_SEPARATOR);

    if (record.getThrown() != null) {
      try {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        record.getThrown().printStackTrace(pw);
        pw.close();
        sb.append(sw.toString());
      } catch (Exception ex) {
        // ignore
      }
    }

    return "RT: " + sb.toString();
  }
 public static void waitStatsThread() throws InterruptedException {
   Thread[] threads = new Thread[20];
   Thread.enumerate(threads);
   for (int i = 0; i < threads.length; i++) {
     Thread currentThread = threads[i];
     if (currentThread != null
         && currentThread.getName().startsWith(StatsManager.RECORDER_THREAD_PREFIX)) {
       currentThread.join();
     }
   }
 }
 protected void endIdegaDatabasePool() {
   PoolManager.getInstance().release();
   int count = Thread.activeCount();
   Thread[] threads = new Thread[count];
   Thread.enumerate(threads);
   for (int i = 0; i < threads.length; i++) {
     Thread thread = threads[i];
     String name = (thread == null) ? null : thread.getName();
     // System.out.println(name);
     if (name != null && name.startsWith("HSQLDB")) {
       thread.interrupt();
     }
   }
 }
 protected void waitForSentNewsletter() throws Exception {
   Thread[] threads = new Thread[40];
   Thread.enumerate(threads);
   for (int i = 0; i < threads.length; i++) {
     Thread currentThread = threads[i];
     if (currentThread != null
         && (currentThread
                 .getName()
                 .startsWith(JpnewsletterSystemConstants.NEWSLETTER_SENDER_THREAD_NAME)
             || currentThread
                 .getName()
                 .startsWith(JpnewsletterSystemConstants.EMAIL_SENDER_NAME_THREAD_PREFIX))) {
       currentThread.join();
     }
   }
 }
Exemple #8
1
 @Override
 public void run() {
   boolean loop = true;
   try {
     while (loop) {
       final Thread[] all = new Thread[Thread.activeCount() + 100];
       final int count = Thread.enumerate(all);
       loop = false;
       for (int i = 0; i < count; i++) {
         final Thread t = all[i];
         // daemon: skip it.
         if (t.isDaemon()) continue;
         // RMI Reaper: skip it.
         if (t.getName().startsWith("RMI Reaper")) continue;
         if (t.getName().startsWith("DestroyJavaVM")) continue;
         // Non daemon, non RMI Reaper: join it, break the for
         // loop, continue in the while loop (loop=true)
         loop = true;
         try {
           // Found a non-daemon thread. Wait for it.
           t.join();
         } catch (Exception ex) {
           ex.printStackTrace();
         }
         break;
       }
     }
     // We went through a whole for-loop without finding any thread
     // to join. We can close cs.
   } catch (Exception ex) {
     ex.printStackTrace();
   } finally {
     try {
       // if we reach here it means the only non-daemon threads
       // that remain are reaper threads - or that we got an
       // unexpected exception/error.
       //
       if (cs != null && cs.isActive()) {
         cs.stop();
       }
     } catch (Exception ex) {
       ex.printStackTrace();
     }
   }
 }
 // Enumerate all active threads
 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;
 }
  // @Test
  public void rawEpochStealing() throws Exception {
    RealEventHubUtilities utils = new RealEventHubUtilities();
    utils.setup(-1);

    int clientSerialNumber = 0;
    while (true) {
      Thread[] blah = new Thread[Thread.activeCount() + 10];
      int actual = Thread.enumerate(blah);
      if (actual >= blah.length) {
        System.out.println("Lost some threads");
      }
      int parkedCount = 0;
      String selectingList = "";
      boolean display = true;
      for (int i = 0; i < actual; i++) {
        display = true;
        StackTraceElement[] bloo = blah[i].getStackTrace();
        String show = "nostack";
        if (bloo.length > 0) {
          show = bloo[0].getClassName() + "." + bloo[0].getMethodName();
          if (show.compareTo("sun.misc.Unsafe.park") == 0) {
            parkedCount++;
            display = false;
          } else if (show.compareTo("sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0") == 0) {
            selectingList += (" " + blah[i].getId());
            display = false;
          }
        }
        if (display) {
          System.out.print(" " + blah[i].getId() + ":" + show);
        }
      }
      System.out.println("\nParked: " + parkedCount + "  SELECTING: " + selectingList);

      System.out.println("Client " + clientSerialNumber + " starting");
      EventHubClient client =
          EventHubClient.createFromConnectionStringSync(utils.getConnectionString().toString());
      PartitionReceiver receiver =
          client
              .createEpochReceiver(
                  utils.getConsumerGroup(), "0", PartitionReceiver.START_OF_STREAM, 1)
              .get();

      boolean useReceiveHandler = false;

      if (useReceiveHandler) {
        Blah b = new Blah(clientSerialNumber++, receiver, client);
        receiver.setReceiveHandler(b).get();
        // wait for messages to start flowing
        b.waitForReceivedMessages().get();
      } else {
        receiver.receiveSync(1);
        System.out.println("Received a message");
      }

      // Enable these lines to avoid overlap
      /* */
      try {
        System.out.println("Non-overlap close of PartitionReceiver");
        if (useReceiveHandler) {
          receiver.setReceiveHandler(null).get();
        }
        receiver.close().get();
      } catch (InterruptedException | ExecutionException e) {
        System.out.println(
            "Client "
                + clientSerialNumber
                + " failed while closing PartitionReceiver: "
                + e.toString());
      }
      try {
        System.out.println("Non-overlap close of EventHubClient");
        client.close().get();
      } catch (InterruptedException | ExecutionException e) {
        System.out.println(
            "Client "
                + clientSerialNumber
                + " failed while closing EventHubClient: "
                + e.toString());
      }
      System.out.println("Client " + clientSerialNumber + " closed");
      /* */

      System.out.println("Threads: " + Thread.activeCount());
    }
  }