Ejemplo n.º 1
0
 /**
  * En este proyecto desarrollaremos una aplicación que calcula el factorial de 10 números
  * distintos. Crearemos un tarea Callable que calcule el factorial de un número y un ejecutor con
  * un grupo de 2 hilos que lance las diez tareas y obtenga el resultado de cada una de ellas,
  * usando objetos Future, y lo almacene en una lista, para finalmente mostrar todos los
  * resultados.
  */
 public static void main(String[] args) {
   // Creo un FixedThreadPool de dos hilos.
   ThreadPoolExecutor ejecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
   // Creo una lista en la que almacenar los objetos Future con el resultado
   // de los hilos.
   List<Future<Integer>> listaResultados = new ArrayList<>();
   // Creo 10 tareas consistente en el cálculo del factorial de números
   // aleatorios menores que 10.
   Random aleatorio = new Random();
   Integer[] numeros = new Integer[10];
   for (int i = 0; i < 10; i++) {
     numeros[i] = aleatorio.nextInt(10);
     CalculadoraFactorial factorial = new CalculadoraFactorial(numeros[i]);
     Future<Integer> futuroResultado = ejecutor.submit(factorial);
     listaResultados.add(futuroResultado);
   }
   // Espero la finalización de las diez tareas.
   do {
     // Mientras espero muestro los objetos Future disponibles.
     System.out.printf("Completas -> %d. Tareas:", ejecutor.getCompletedTaskCount());
     for (int i = 0; i < listaResultados.size(); i++) {
       if (listaResultados.get(i).isDone()) {
         System.out.printf(" %d", i);
       }
     }
     System.out.printf("\n");
     // Duermo durante unas milésimas para volver a comprobar.
     try {
       Thread.sleep(50);
     } catch (InterruptedException e) {
       e.printStackTrace();
     }
   } while (ejecutor.getCompletedTaskCount() < listaResultados.size());
   // Muestro los resultados
   System.out.printf("Resultados:\n");
   Integer valor = null;
   for (int i = 0; i < listaResultados.size(); i++) {
     // Obtengo el objeto future de la lista.
     Future<Integer> objetoResultado = listaResultados.get(i);
     try {
       valor = objetoResultado.get();
     } catch (InterruptedException | ExecutionException e) {
       e.printStackTrace();
     }
     System.out.printf("Tarea %d -> Factorial de %d = %d\n", i, numeros[i], valor);
   }
   // Finalizo el ejecutor.
   ejecutor.shutdown();
 }
Ejemplo 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);
 }
Ejemplo n.º 3
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());
 }
Ejemplo n.º 4
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;
 }
Ejemplo n.º 5
0
  @Test(timeout = 60000)
  public void testPriorityRegionIsOpenedWithSeparateThreadPool() throws Exception {
    ThreadPoolExecutor exec =
        getRS().getExecutorService().getExecutorThreadPool(ExecutorType.RS_OPEN_PRIORITY_REGION);

    assertEquals(0, exec.getCompletedTaskCount());

    HTableDescriptor htd = new HTableDescriptor(tableName);
    htd.setPriority(HConstants.HIGH_QOS);
    htd.addFamily(new HColumnDescriptor(HConstants.CATALOG_FAMILY));
    try (Connection connection = ConnectionFactory.createConnection(HTU.getConfiguration());
        Admin admin = connection.getAdmin()) {
      admin.createTable(htd);
    }

    assertEquals(1, exec.getCompletedTaskCount());
  }
Ejemplo n.º 6
0
 @Override
 public String toString() {
   return "JobManager{done="
       + service.getCompletedTaskCount()
       + ", active="
       + service.getActiveCount()
       + ", total="
       + service.getTaskCount()
       + "}";
 }
Ejemplo n.º 7
0
 public void abortConnection() throws SQLException {
   Connection connection = DriverManager.getConnection("jdbc:derby://localhost/java7book");
   ThreadPoolExecutor executor =
       new DebugExecutorService(2, 10, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
   connection.abort(executor);
   executor.shutdown();
   try {
     executor.awaitTermination(5, TimeUnit.MINUTES);
     System.out.println(executor.getCompletedTaskCount());
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 8
0
  public void run() {
    while (true) {
      ThreadPoolExecutor threadTemp = (ThreadPoolExecutor) serverView.getExecutor();
      serverView.setActiveThreadCount(threadTemp.getActiveCount());
      serverView.setTaskDealedCount(threadTemp.getCompletedTaskCount());
      serverView.setCurrentTaskCount(threadTemp.getTaskCount());
      serverView.ThreadShow();

      try {
        Thread.sleep(period);
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
Ejemplo n.º 9
0
  /*
  并发压力测试
  * */
  @Test
  public void testGetTraceId_MutilTreand() throws Exception {
    ThreadPoolExecutor threadPool;
    // 并发数
    int concurrent = 10;
    // 每并发请求数
    long commint = 100 * 1000;
    int rounds = 3; // 测试轮数
    threadPool =
        new ThreadPoolExecutor(
            concurrent,
            concurrent,
            1,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(30),
            new ThreadPoolExecutor.DiscardOldestPolicy());
    long sumResult = 0;
    int completeTask = 0;
    for (int k : new int[rounds]) {
      long startTime = System.currentTimeMillis();
      // 创建traceId申请请求队列
      for (int i = 0; i < concurrent; i++) {
        try {
          RequestTasker task = new RequestTasker(i, commint);
          threadPool.execute(task);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      while (threadPool.getCompletedTaskCount() - completeTask < concurrent) {
        Thread.sleep(5);
      }
      long endTime = System.currentTimeMillis();
      sumResult += (endTime - startTime);
      completeTask += concurrent;
    }

    System.out.println("多线程并发测试");
    System.out.println("轮数=" + rounds);
    System.out.println("每轮提交量=" + concurrent * commint);
    System.out.println("平均耗时=" + sumResult / rounds + " ms");
    System.out.printf("Tps=%.1f 次/ms\n", 1.0 * concurrent * commint / (sumResult / rounds));
    threadPool.shutdown();
    System.out.println("------------------------");
    Thread.sleep(10);
  }
Ejemplo n.º 10
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);
    }
  }
 public long getCompletedTaskCount() {
   return (executor != null) ? executor.getCompletedTaskCount() : 0;
 }
Ejemplo n.º 12
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 long getTaskCount() {
   return (executorService_.getTaskCount() - executorService_.getCompletedTaskCount());
 }
  public void runScenario() {
    MultiThreadedTaskRunner taskRunner = TestEnvironment.getMultiThreadedTaskRunner();

    ThreadPoolExecutor executorService = (ThreadPoolExecutor) taskRunner.getExecutorService();
    assertEquals(TestEnvironment.THREAD_COUNT, executorService.getMaximumPoolSize());
    assertEquals(0, executorService.getActiveCount());

    DataCleanerConfiguration configuration =
        new DataCleanerConfigurationImpl()
            .withEnvironment(new DataCleanerEnvironmentImpl().withTaskRunner(taskRunner));

    AnalysisRunner runner = new AnalysisRunnerImpl(configuration);

    Datastore ds = TestHelper.createSampleDatabaseDatastore("foobar");
    try (DatastoreConnection con = ds.openConnection()) {

      AnalysisJob job;
      try (AnalysisJobBuilder analysisJobBuilder = new AnalysisJobBuilder(configuration)) {
        analysisJobBuilder.setDatastore(ds);

        Table table = con.getDataContext().getDefaultSchema().getTableByName("ORDERFACT");
        assertNotNull(table);

        Column statusColumn = table.getColumnByName("STATUS");
        Column commentsColumn = table.getColumnByName("COMMENTS");

        analysisJobBuilder.addSourceColumns(statusColumn, commentsColumn);
        analysisJobBuilder
            .addAnalyzer(AnalyzerMock.class)
            .addInputColumns(analysisJobBuilder.getSourceColumns());

        job = analysisJobBuilder.toAnalysisJob();
      }

      AnalysisResultFuture resultFuture = runner.run(job);

      try {
        Thread.sleep(550);
      } catch (InterruptedException e) {
        e.printStackTrace();
        fail("Interrupted! " + e.getMessage());
      }

      resultFuture.cancel();

      assertFalse(resultFuture.isSuccessful());
      assertTrue(resultFuture.isCancelled());
      assertTrue(resultFuture.isErrornous());

      try {
        Thread.sleep(400);
      } catch (InterruptedException e) {
        e.printStackTrace();
        fail("Interrupted! " + e.getMessage());
      }

      assertEquals(TestEnvironment.THREAD_COUNT, executorService.getMaximumPoolSize());

      long completedTaskCount = executorService.getCompletedTaskCount();
      assertTrue("completedTaskCount was: " + completedTaskCount, completedTaskCount > 3);

      int largestPoolSize = executorService.getLargestPoolSize();
      assertTrue("largestPoolSize was: " + largestPoolSize, largestPoolSize > 5);
      assertEquals(0, executorService.getActiveCount());
    }

    taskRunner.shutdown();
  }