Esempio n. 1
0
 /* ------------------------------------------------------------ */
 public boolean isLowOnThreads() {
   if (_executor instanceof ThreadPoolExecutor) {
     final ThreadPoolExecutor tpe = (ThreadPoolExecutor) _executor;
     // getActiveCount() locks the thread pool, so execute it last
     return tpe.getPoolSize() == tpe.getMaximumPoolSize()
         && tpe.getQueue().size() >= tpe.getPoolSize() - tpe.getActiveCount();
   }
   return false;
 }
Esempio n. 2
0
 public ThreadPoolStats stats() {
   List<ThreadPoolStats.Stats> stats = new ArrayList<ThreadPoolStats.Stats>();
   for (ExecutorHolder holder : executors.values()) {
     String name = holder.info.getName();
     // no need to have info on "same" thread pool
     if ("same".equals(name)) {
       continue;
     }
     int threads = -1;
     int queue = -1;
     int active = -1;
     long rejected = -1;
     int largest = -1;
     long completed = -1;
     if (holder.executor instanceof ThreadPoolExecutor) {
       ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) holder.executor;
       threads = threadPoolExecutor.getPoolSize();
       queue = threadPoolExecutor.getQueue().size();
       active = threadPoolExecutor.getActiveCount();
       largest = threadPoolExecutor.getLargestPoolSize();
       completed = threadPoolExecutor.getCompletedTaskCount();
       RejectedExecutionHandler rejectedExecutionHandler =
           threadPoolExecutor.getRejectedExecutionHandler();
       if (rejectedExecutionHandler instanceof XRejectedExecutionHandler) {
         rejected = ((XRejectedExecutionHandler) rejectedExecutionHandler).rejected();
       }
     }
     stats.add(
         new ThreadPoolStats.Stats(name, threads, queue, active, rejected, largest, completed));
   }
   return new ThreadPoolStats(stats);
 }
Esempio n. 3
0
  private static void realMain(String[] args) throws Throwable {
    final int n = 4;
    final CyclicBarrier barrier = new CyclicBarrier(2 * n + 1);
    final ThreadPoolExecutor pool =
        new ThreadPoolExecutor(
            n, 2 * n, KEEPALIVE_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
    final Runnable r =
        new Runnable() {
          public void run() {
            try {
              barrier.await();
              barrier.await();
            } catch (Throwable t) {
              unexpected(t);
            }
          }
        };

    for (int i = 0; i < 2 * n; i++) pool.execute(r);
    barrier.await();
    checkPoolSizes(pool, 2 * n, n, 2 * n);
    barrier.await();
    long nap = KEEPALIVE_MS + (KEEPALIVE_MS >> 2);
    for (long sleepyTime = 0L; pool.getPoolSize() > n; ) {
      check((sleepyTime += nap) <= LONG_DELAY_MS);
      Thread.sleep(nap);
    }
    checkPoolSizes(pool, n, n, 2 * n);
    Thread.sleep(nap);
    checkPoolSizes(pool, n, n, 2 * n);
    pool.shutdown();
    check(pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
  }
Esempio n. 4
0
 /* ------------------------------------------------------------ */
 public int getThreads() {
   if (_executor instanceof ThreadPoolExecutor) {
     final ThreadPoolExecutor tpe = (ThreadPoolExecutor) _executor;
     return tpe.getPoolSize();
   }
   return -1;
 }
Esempio n. 5
0
 /* ------------------------------------------------------------ */
 public int getIdleThreads() {
   if (_executor instanceof ThreadPoolExecutor) {
     final ThreadPoolExecutor tpe = (ThreadPoolExecutor) _executor;
     return tpe.getPoolSize() - tpe.getActiveCount();
   }
   return -1;
 }
 @Override
 public Map<String, String> handleProbe(String... keys) {
   if (keys == null || keys.length == 0) {
     return null;
   }
   ThreadPoolExecutor exec = extract(executor);
   if (exec == null) {
     return null;
   }
   Map<String, String> map = new HashMap<>();
   for (String key : keys) {
     switch (key) {
       case KEY:
         map.put("active-thread", String.valueOf(exec.getActiveCount()));
         map.put("min-thread", String.valueOf(exec.getCorePoolSize()));
         map.put("max-thread", String.valueOf(exec.getMaximumPoolSize()));
         map.put("current-pool-size", String.valueOf(exec.getPoolSize()));
         map.put("largest-pool-size", String.valueOf(exec.getLargestPoolSize()));
         map.put("keep-alive", String.valueOf(exec.getKeepAliveTime(TimeUnit.MILLISECONDS)));
         map.put("queue-size", String.valueOf(exec.getQueue().size()));
         break;
     }
   }
   return map.isEmpty() ? null : map;
 }
 public boolean offer(Runnable o) {
   // we can't do any checks
   if (parent == null) return super.offer(o);
   int poolSize = parent.getPoolSize();
   // we are maxed out on threads, simply queue the object
   if (parent.getPoolSize() == parent.getMaximumPoolSize()) return super.offer(o);
   // we have idle threads, just add it to the queue
   // note that we don't use getActiveCount(), see BZ 49730
   AtomicInteger submittedTasksCount = StandardThreadExecutor.this.submittedTasksCount;
   if (submittedTasksCount != null) {
     if (submittedTasksCount.get() <= poolSize) return super.offer(o);
   }
   // if we have less threads than maximum force creation of a new thread
   if (poolSize < parent.getMaximumPoolSize()) return false;
   // if we reached here, we need to add it to the queue
   return super.offer(o);
 }
Esempio n. 8
0
 public void executeTask(Task task) {
   System.out.printf("Server: A new task has arrived\n");
   executor.execute(task);
   System.out.printf("Server: Task count: %d \n", executor.getTaskCount());
   System.out.printf("Server: Pool size: %d \n", executor.getPoolSize());
   System.out.printf("Server: Active count: %d \n", executor.getActiveCount());
   System.out.printf("Server: Compeleted task : %d \n", executor.getCompletedTaskCount());
 }
Esempio n. 9
0
 public Future executeTask(Task task) {
   System.out.println("Server: new task has arrived \n");
   Future future = (Future) executor.submit(task);
   System.out.printf("Server:%s Pool Size \n", executor.getPoolSize());
   System.out.printf("Server:%s Active thread \n", executor.getActiveCount());
   System.out.printf("Server:%s Completed Tasks \n", executor.getCompletedTaskCount());
   return future;
 }
  @Test
  public void testScaleDown() throws Exception {
    final int min = between(1, 3);
    final int max = between(min + 1, 6);
    final ThreadBarrier barrier = new ThreadBarrier(max + 1);

    final ThreadPoolExecutor pool =
        EsExecutors.newScaling(
            min,
            max,
            between(1, 100),
            TimeUnit.MILLISECONDS,
            EsExecutors.daemonThreadFactory("test"));
    assertThat("Min property", pool.getCorePoolSize(), equalTo(min));
    assertThat("Max property", pool.getMaximumPoolSize(), equalTo(max));

    for (int i = 0; i < max; ++i) {
      final CountDownLatch latch = new CountDownLatch(1);
      pool.execute(
          new Runnable() {
            @Override
            public void run() {
              latch.countDown();
              try {
                barrier.await();
                barrier.await();
              } catch (Throwable e) {
                barrier.reset(e);
              }
            }
          });

      // wait until thread executes this task
      // otherwise, a task might be queued
      latch.await();
    }

    barrier.await();
    assertThat("wrong pool size", pool.getPoolSize(), equalTo(max));
    assertThat("wrong active size", pool.getActiveCount(), equalTo(max));
    barrier.await();
    assertBusy(
        new Runnable() {
          @Override
          public void run() {
            assertThat("wrong active count", pool.getActiveCount(), equalTo(0));
            assertThat(
                "idle threads didn't shrink below max. (" + pool.getPoolSize() + ")",
                pool.getPoolSize(),
                lessThan(max));
          }
        });
    terminate(pool);
  }
  @Override
  public final void messageReceived(
      final ChannelHandlerContext ctx, final MessageEvent messageEvent)
      throws InvalidProtocolBufferException {
    final Messages.Msg message = (Messages.Msg) messageEvent.getMessage();
    if (WSNApp.MSG_TYPE_LISTENER_MESSAGE.equals(message.getMsgType())) {
      final WSNAppMessages.Message wsnAppMessage =
          WSNAppMessages.Message.parseFrom(message.getPayload());
      parse(wsnAppMessage.toString());
      messageCounter++;
      if (messageCounter == REPORT_LIMIT) {
        final long milliseconds = System.currentTimeMillis() - lastTime;
        final double stat = messageCounter / (milliseconds / (double) REPORT_LIMIT);
        LOGGER.info("MessageRate : " + stat + " messages/sec");
        final ThreadPoolExecutor pool = (ThreadPoolExecutor) executorService;
        LOGGER.info("PoolSize : " + pool.getPoolSize() + " Active :" + pool.getActiveCount());
        LOGGER.info("Peak : " + pool.getLargestPoolSize());
        final NodeReading nodeReading = new NodeReading();
        nodeReading.setTestbedId("3");
        nodeReading.setNodeId("urn:ctinetwork:uberdust");
        nodeReading.setCapabilityName("urn:ctinetwork:testbedlistener:poolSize");
        nodeReading.setReading(String.valueOf(pool.getPoolSize()));
        nodeReading.setTimestamp(String.valueOf((new Date()).getTime()));
        new WsCommiter(nodeReading);
        nodeReading.setCapabilityName("urn:ctinetwork:testbedlistener:activeThreads");
        nodeReading.setReading(String.valueOf(pool.getActiveCount()));
        new WsCommiter(nodeReading);
        nodeReading.setCapabilityName("urn:ctinetwork:testbedlistener:messageRate");
        nodeReading.setReading(String.valueOf(stat));
        new WsCommiter(nodeReading);

        lastTime = System.currentTimeMillis();
        messageCounter = 0;
      }

    } else {
      LOGGER.error("got a message of type " + message.getMsgType());
    }
  }
Esempio n. 12
0
  public void run() {

    AtomCache cache = new AtomCache(config);

    StructureAlignment algorithm = null;
    if (parent != null) algorithm = parent.getStructureAlignment();
    else {
      algorithm = customAlgorithm;
    }
    String serverLocation = FarmJobParameters.DEFAULT_SERVER_URL;
    if (representatives == null) {
      SortedSet<String> repre = JFatCatClient.getRepresentatives(serverLocation, 40);
      System.out.println("got  " + repre.size() + " representatives for comparison");
      representatives = repre;
    }

    String header = "# algorithm:" + algorithm.getAlgorithmName();

    String legend =
        "# name1\tname2\tscore\tprobability\trmsd\tlen1\tlen2\tcov1\tcov2\t%ID\tDescription\t ";
    if (algorithm.getAlgorithmName().equalsIgnoreCase(CeMain.algorithmName)
        || algorithm.getAlgorithmName().equalsIgnoreCase(CeSideChainMain.algorithmName)) {
      legend = "# name1\tname2\tscore\tz-score\trmsd\tlen1\tlen2\tcov1\tcov2\t%ID\tDescription\t ";
    }

    File outFileF = new File(outFile);
    if (!outFileF.isDirectory()) {
      System.err.println(
          outFileF.getAbsolutePath()
              + " is not a directory, can't create result files in there... ");
      interrupt();
      cleanup();
    }

    if (name1 == null) name1 = "CUSTOM";

    SynchronizedOutFile out;

    resultList = new File(outFileF, "results_" + name1 + ".out");

    try {

      out = new SynchronizedOutFile(resultList);

      out.write(header);
      out.write(AFPChain.newline);
      out.write(legend);
      out.write(AFPChain.newline);

      if (name1.equals("CUSTOM")) {

        String config1 = "#param:file1=" + parent.getDBSearch().getPDBUploadPanel().getFilePath1();
        out.write(config1);
        out.write(AFPChain.newline);

        String config2 = "#param:chain1=" + parent.getDBSearch().getPDBUploadPanel().getChain1();
        out.write(config2);
        out.write(AFPChain.newline);
      }

      if (algorithm.getAlgorithmName().startsWith("jCE")) {
        ConfigStrucAligParams params = algorithm.getParameters();
        if (params instanceof CeParameters) {
          CeParameters ceParams = (CeParameters) params;
          if (ceParams.getScoringStrategy() != CeParameters.DEFAULT_SCORING_STRATEGY) {
            String scoring = "#param:scoring=" + ceParams.getScoringStrategy();
            out.write(scoring);
            out.write(AFPChain.newline);
          }
        }
      }

    } catch (Exception e) {
      System.err.println("Error while loading representative structure " + name1);
      e.printStackTrace();
      interrupt();
      cleanup();
      return;
    }

    DomainProvider domainProvider = DomainProviderFactory.getDomainProvider();

    ConcurrencyTools.setThreadPoolSize(nrCPUs);

    Atom[] ca1 = StructureTools.getAtomCAArray(structure1);

    int nrJobs = 0;
    for (String repre : representatives) {

      if (domainSplit) {
        SortedSet<String> domainNames = domainProvider.getDomainNames(repre);
        // System.out.println(repre +" got domains: " +domainNames);
        if (domainNames == null || domainNames.size() == 0) {
          // no domains found, use whole chain.
          submit(name1, repre, ca1, algorithm, outFileF, out, cache);
          nrJobs++;
          continue;
        }
        // System.out.println("got " + domainNames.size() + " for " + repre);
        for (String domain : domainNames) {
          submit(name1, domain, ca1, algorithm, outFileF, out, cache);
          nrJobs++;
        }
      } else {
        submit(name1, repre, ca1, algorithm, outFileF, out, cache);
        nrJobs++;
      }
    }

    ThreadPoolExecutor pool = ConcurrencyTools.getThreadPool();
    System.out.println(pool.getPoolSize());

    long startTime = System.currentTimeMillis();

    try {
      while (pool.getCompletedTaskCount() < nrJobs - 1) {
        // long now = System.currentTimeMillis();
        // System.out.println( pool.getCompletedTaskCount() + " " + (now-startTime)/1000 + " " +
        // pool.getPoolSize() + " " + pool.getActiveCount()  + " " + pool.getTaskCount()  );
        //				if ((now-startTime)/1000 > 60) {
        //
        //					interrupt();
        //					System.out.println("completed: " + pool.getCompletedTaskCount());
        //				}

        if (interrupted.get()) break;

        Thread.sleep(2000);
      }
      out.close();
    } catch (Exception e) {
      e.printStackTrace();
      interrupt();
      cleanup();
    }

    if (domainProvider instanceof RemoteDomainProvider) {
      RemoteDomainProvider remote = (RemoteDomainProvider) domainProvider;
      remote.flushCache();
    }
    long now = System.currentTimeMillis();
    System.out.println("Calculation took : " + (now - startTime) / 1000 + " sec.");
    System.out.println(
        pool.getCompletedTaskCount()
            + " "
            + pool.getPoolSize()
            + " "
            + pool.getActiveCount()
            + " "
            + pool.getTaskCount());
    //		if ((now-startTime)/1000 > 30) {

    //		try {
    //			out.flush();
    //			out.close();
    //		} catch (Exception e) {
    //			e.printStackTrace();
    //		}
    if (parent != null) {
      parent.notifyCalcFinished();
      DBResultTable table = new DBResultTable();
      table.show(resultList, config);
    }
  }
Esempio n. 13
0
  public void ejecutarTarea(Tarea tarea) {
    ejecutor.execute(tarea);

    System.out.printf("Servidor -> Tamaño Grupo: %d\n", ejecutor.getPoolSize());
    System.out.printf("Servidor -> Hilos activos: %d\n", ejecutor.getActiveCount());
  }
Esempio n. 14
0
 static void checkPoolSizes(ThreadPoolExecutor pool, int size, int core, int max) {
   equal(pool.getPoolSize(), size);
   equal(pool.getCorePoolSize(), core);
   equal(pool.getMaximumPoolSize(), max);
 }
Esempio n. 15
0
 public String[] getStats() {
   return new String[] {
     "STP:",
     " + Effects:",
     " |- ActiveThreads:   " + _effectsScheduledThreadPool.getActiveCount(),
     " |- getCorePoolSize: " + _effectsScheduledThreadPool.getCorePoolSize(),
     " |- PoolSize:        " + _effectsScheduledThreadPool.getPoolSize(),
     " |- MaximumPoolSize: " + _effectsScheduledThreadPool.getMaximumPoolSize(),
     " |- CompletedTasks:  " + _effectsScheduledThreadPool.getCompletedTaskCount(),
     " |- ScheduledTasks:  "
         + (_effectsScheduledThreadPool.getTaskCount()
             - _effectsScheduledThreadPool.getCompletedTaskCount()),
     " | -------",
     " + General:",
     " |- ActiveThreads:   " + _generalScheduledThreadPool.getActiveCount(),
     " |- getCorePoolSize: " + _generalScheduledThreadPool.getCorePoolSize(),
     " |- PoolSize:        " + _generalScheduledThreadPool.getPoolSize(),
     " |- MaximumPoolSize: " + _generalScheduledThreadPool.getMaximumPoolSize(),
     " |- CompletedTasks:  " + _generalScheduledThreadPool.getCompletedTaskCount(),
     " |- ScheduledTasks:  "
         + (_generalScheduledThreadPool.getTaskCount()
             - _generalScheduledThreadPool.getCompletedTaskCount()),
     " | -------",
     " + AI:",
     " |- ActiveThreads:   " + _aiScheduledThreadPool.getActiveCount(),
     " |- getCorePoolSize: " + _aiScheduledThreadPool.getCorePoolSize(),
     " |- PoolSize:        " + _aiScheduledThreadPool.getPoolSize(),
     " |- MaximumPoolSize: " + _aiScheduledThreadPool.getMaximumPoolSize(),
     " |- CompletedTasks:  " + _aiScheduledThreadPool.getCompletedTaskCount(),
     " |- ScheduledTasks:  "
         + (_aiScheduledThreadPool.getTaskCount()
             - _aiScheduledThreadPool.getCompletedTaskCount()),
     "TP:",
     " + Packets:",
     " |- ActiveThreads:   " + _generalPacketsThreadPool.getActiveCount(),
     " |- getCorePoolSize: " + _generalPacketsThreadPool.getCorePoolSize(),
     " |- MaximumPoolSize: " + _generalPacketsThreadPool.getMaximumPoolSize(),
     " |- LargestPoolSize: " + _generalPacketsThreadPool.getLargestPoolSize(),
     " |- PoolSize:        " + _generalPacketsThreadPool.getPoolSize(),
     " |- CompletedTasks:  " + _generalPacketsThreadPool.getCompletedTaskCount(),
     " |- QueuedTasks:     " + _generalPacketsThreadPool.getQueue().size(),
     " | -------",
     " + I/O Packets:",
     " |- ActiveThreads:   " + _ioPacketsThreadPool.getActiveCount(),
     " |- getCorePoolSize: " + _ioPacketsThreadPool.getCorePoolSize(),
     " |- MaximumPoolSize: " + _ioPacketsThreadPool.getMaximumPoolSize(),
     " |- LargestPoolSize: " + _ioPacketsThreadPool.getLargestPoolSize(),
     " |- PoolSize:        " + _ioPacketsThreadPool.getPoolSize(),
     " |- CompletedTasks:  " + _ioPacketsThreadPool.getCompletedTaskCount(),
     " |- QueuedTasks:     " + _ioPacketsThreadPool.getQueue().size(),
     " | -------",
     " + General Tasks:",
     " |- ActiveThreads:   " + _generalThreadPool.getActiveCount(),
     " |- getCorePoolSize: " + _generalThreadPool.getCorePoolSize(),
     " |- MaximumPoolSize: " + _generalThreadPool.getMaximumPoolSize(),
     " |- LargestPoolSize: " + _generalThreadPool.getLargestPoolSize(),
     " |- PoolSize:        " + _generalThreadPool.getPoolSize(),
     " |- CompletedTasks:  " + _generalThreadPool.getCompletedTaskCount(),
     " |- QueuedTasks:     " + _generalThreadPool.getQueue().size(),
     " | -------",
     " + Javolution stats:",
     " |- FastList:        " + FastList.report(),
     " |- FastMap:        " + FastMap.report(),
     " |- FastSet:        " + FastSet.report(),
     " | -------"
   };
 }
 public int getThreads() {
   return executor.getPoolSize();
 }
 public int getIdleThreads() {
   return executor.getPoolSize() - executor.getActiveCount();
 }
Esempio n. 18
0
 public int getPoolActiveThreadCount() {
   if (pool != null) {
     return pool.getPoolSize();
   }
   return -1;
 } // getPoolActiveThreadCount
Esempio n. 19
0
 public int getCurrentThreads() {
   return pool.getPoolSize();
 }
 public int getPoolSize() {
   return (executor != null) ? executor.getPoolSize() : 0;
 }