Exemplo n.º 1
0
 /** Completed submit(ForkJoinTask) returns result */
 public void testSubmitForkJoinTask() throws Throwable {
   ForkJoinPool p = new ForkJoinPool(1);
   try (PoolCleaner cleaner = cleaner(p)) {
     ForkJoinTask<Integer> f = p.submit(new FibTask(8));
     assertEquals(21, (int) f.get());
   }
 }
Exemplo n.º 2
0
 @Override
 public Double Sum(int[] elts) {
   ForkJoinPool pool = new ForkJoinPool();
   Double result = pool.invoke(new SumTask(elts, 0, elts.length));
   pool.shutdown();
   return result;
 }
 /**
  * Start the ingest procedure.
  *
  * @param iterator the iterator to iterate on
  * @return the pid of the root object, or null of something odd failed
  */
 @Override
 public String ingest(TreeIterator iterator) {
   this.iterator = iterator;
   ForkJoinPool forkJoinPool = new ForkJoinPool(concurrency);
   ForkJoinTask<String> result;
   result = forkJoinPool.submit(this);
   forkJoinPool.shutdown();
   try {
     return result.get();
   } catch (CancellationException | ExecutionException | InterruptedException e) {
     log.warn("Shutting down pool {}", forkJoinPool);
     result.cancel(true);
     forkJoinPool.shutdownNow();
     boolean shutdown;
     try {
       shutdown = forkJoinPool.awaitTermination(3, TimeUnit.MINUTES);
     } catch (InterruptedException e1) {
       shutdown = false;
     }
     if (!shutdown) {
       log.error("Failed to shut down forkjoinpool {}", forkJoinPool);
       System.exit(1);
     }
     log.debug("Pool shot down {}", forkJoinPool);
     throw new IngesterShutdownException(e);
   }
 }
Exemplo n.º 4
0
  /**
   * Main method of the example
   *
   * @param args
   */
  public static void main(String[] args) {

    // Generate an array of 1000 integers
    ArrayGenerator generator = new ArrayGenerator();
    int array[] = generator.generateArray(1000);

    // Create a TaskManager object
    TaskManager manager = new TaskManager();

    // Create a ForkJoinPool with the default constructor
    ForkJoinPool pool = new ForkJoinPool();

    // Create a Task to process the array
    SearchNumberTask task = new SearchNumberTask(array, 0, 1000, 5, manager);

    // Execute the task
    pool.execute(task);

    // Shutdown the pool
    pool.shutdown();

    // Wait for the finalization of the task
    try {
      pool.awaitTermination(1, TimeUnit.DAYS);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    // Write a message to indicate the end of the program
    System.out.printf("Main: The program has finished\n");
  }
Exemplo n.º 5
0
  @Override
  public AbstractFeature build() throws JATEException {
    List<String> contextIds = new ArrayList<>(frequencyCtxBased.getMapCtx2TTF().keySet());
    // start workers
    int cores = Runtime.getRuntime().availableProcessors();
    cores = (int) (cores * properties.getFeatureBuilderMaxCPUsage());
    cores = cores == 0 ? 1 : cores;
    StringBuilder sb = new StringBuilder("Building features using cpu cores=");
    sb.append(cores)
        .append(", total ctx=")
        .append(contextIds.size())
        .append(", max per worker=")
        .append(properties.getFeatureBuilderMaxDocsPerWorker());
    LOG.info(sb.toString());
    CooccurrenceFBWorker worker =
        new CooccurrenceFBWorker(
            contextIds,
            frequencyTermBased,
            minTTF,
            frequencyCtxBased,
            minTCF,
            properties.getFeatureBuilderMaxTermsPerWorker());
    LOG.info("Filtering candidates with min.ttf=" + minTTF + " min.tcf=" + minTCF);
    ForkJoinPool forkJoinPool = new ForkJoinPool(cores);
    Cooccurrence feature = forkJoinPool.invoke(worker);
    sb = new StringBuilder("Complete building features.");
    LOG.info(sb.toString());

    return feature;
  }
Exemplo n.º 6
0
  public static void downloadEncrypted(List<NUSTitleInformation> output_, final Progress progress) {
    ForkJoinPool pool = ForkJoinPool.commonPool();
    List<ForkJoinTask<Boolean>> list = new ArrayList<>();

    for (final NUSTitleInformation nus : output_) {
      final long tID = nus.getTitleID();
      list.add(
          pool.submit(
              new Callable<Boolean>() {
                @Override
                public Boolean call() throws Exception {
                  NUSTitle nusa =
                      new NUSTitle(
                          tID, nus.getSelectedVersion(), Util.ByteArrayToString(nus.getKey()));
                  Progress childProgress = new Progress();
                  progress.add(childProgress);
                  nusa.downloadEncryptedFiles(progress);

                  return true;
                }
              }));
    }
    for (ForkJoinTask<Boolean> task : list) {
      try {
        task.get();
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
Exemplo n.º 7
0
  public static void main(String[] args) throws Exception {
    // 8、创建ForkJoinPool对象
    ForkJoinPool pool = new ForkJoinPool();

    // 9、创建元素有10000个的int数组
    int array[] = new int[10000];

    // 10、创建一个新的任务,处理整个数组
    Task task1 = new Task(array, 0, array.length);

    // 11、使用execute()方法将任务发给pool执行,
    pool.execute(task1);

    // 12、当任务未结束时,调用showLog()方法输出ForkJoinPool类的状态信息,并使线程睡眠1秒
    while (!task1.isDone()) {
      showLog(pool);
      TimeUnit.SECONDS.sleep(1);
    }

    // 13、使用shutDown()y方法关闭pool
    pool.shutdown();

    // 14、使用awaitTermination()方法等待pool结束
    pool.awaitTermination(1, TimeUnit.DAYS);

    // 15、调用showLog()方法,输出ForkJoinPool类的状态信息,并输出程序执行结束的信息
    showLog(pool);
    System.out.printf("Main: End of the program.\n");
  }
Exemplo n.º 8
0
  public static BufferedImage blur(BufferedImage srcImage) {
    int w = srcImage.getWidth();
    int h = srcImage.getHeight();

    int[] src = srcImage.getRGB(0, 0, w, h, null, 0, w);
    int[] dst = new int[src.length];

    System.out.println("Array size is " + src.length);
    System.out.println("Threshold is " + sThreshold);

    int processors = Runtime.getRuntime().availableProcessors();
    System.out.println(
        Integer.toString(processors)
            + " processor"
            + (processors != 1 ? "s are " : " is ")
            + "available");

    ForkBlur fb = new ForkBlur(src, 0, src.length, dst);

    ForkJoinPool pool = new ForkJoinPool();

    long startTime = System.currentTimeMillis();
    pool.invoke(fb);
    long endTime = System.currentTimeMillis();

    System.out.println("Image blur took " + (endTime - startTime) + " milliseconds.");

    BufferedImage dstImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    dstImage.setRGB(0, 0, w, h, dst, 0, w);

    return dstImage;
  }
Exemplo n.º 9
0
 public static void main(String[] args) {
   int processors = Runtime.getRuntime().availableProcessors();
   System.out.println("Number of processors: " + processors);
   Fibonacci3 f = new Fibonacci3(Integer.parseInt(args[0]));
   ForkJoinPool pool = new ForkJoinPool(processors);
   int result = pool.invoke(f);
   System.out.println("Result: " + result);
 }
Exemplo n.º 10
0
 /**
  * A default-constructed SubmissionPublisher has no subscribers, is not closed, has default buffer
  * size, and uses the defaultExecutor
  */
 public void testConstructor1() {
   SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer>();
   checkInitialState(p);
   assertEquals(p.getMaxBufferCapacity(), Flow.defaultBufferSize());
   Executor e = p.getExecutor(), c = ForkJoinPool.commonPool();
   if (ForkJoinPool.getCommonPoolParallelism() > 1) assertSame(e, c);
   else assertNotSame(e, c);
 }
 public static void main(String[] args) throws Exception {
   ForkJoinPool pool = new ForkJoinPool();
   // 提交可分解的PrintTask任务
   pool.submit(new PrintTask(0, 600));
   pool.awaitTermination(2, TimeUnit.SECONDS);
   // 关闭线程池
   pool.shutdown();
 }
Exemplo n.º 12
0
  public static void main(String[] args) {
    arrayToSearch = new int[N];

    for (int i = 0; i < N; i++) {
      arrayToSearch[i] = ThreadLocalRandom.current().nextInt(0, 1000);
    }
    ForkJoinPool pool = new ForkJoinPool(NUM_THREADS);
    pool.invoke(new SearchTask(0, N - 1));
  }
Exemplo n.º 13
0
  @Override
  public synchronized void doTick(final int tps) {
    if (this.groups.isEmpty()) {
      if (!CoreMain.isClient()) {
        SpammyError.err(
            "There is no tick groups, server don't have anything to do. Do you have any worlds?",
            10,
            key);
      }
      return;
    }
    if (this.groups.size() == 1) {
      /** TODO count time of execution and split if needed. */
      this.groups.iterator().next().doTick(tps);
      return;
    }
    final AtomicInteger i = new AtomicInteger(0);
    final ForkJoinPool pool =
        new ForkJoinPool(
            this.groups.size(),
            p -> new NamedForkJoinWorkerThread(p, i.getAndIncrement()),
            (t, e) -> {
              // TODO: maybe add some pretty error priting
              System.err.println("Error in tick thread: " + t.getName());
              e.printStackTrace();
            },
            false);

    /**
     * TODO count time of execution for all groups. if any group is creating lags, try split it.
     * (should not count single-time lags?) if two grups can be join, try join them.
     */
    final CountDownLatch latch = new CountDownLatch(this.groups.size());
    for (final Iterator<TickGroupImpl> it = this.groups.iterator(); it.hasNext(); ) {
      final TickGroupImpl tickGroup = it.next();
      if (tickGroup.isEmpty()) {
        it.remove();
        latch.countDown();
        continue;
      }
      pool.submit(
          () -> {
            try {
              tickGroup.doTick(tps);
              this.core.runScheduler(false);
              this.core.runSync();
            } finally {
              latch.countDown();
            }
          });
    }
    try {
      latch.await();
    } catch (final InterruptedException e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 14
0
  public static void main(String[] args) {
    int[] nums = new int[] {5, 4, 6, 2, 8, 7, 9, 1, 3};

    ForkJoinPool pool = new ForkJoinPool();
    ForkJoinTask<Void> t = pool.submit(new ForkJoinFastSort(nums, 0, 8));
    t.join();
    for (int i : nums) {
      System.err.print(i);
    }
  }
Exemplo n.º 15
0
  public static void main(String[] args) throws InterruptedException, ExecutionException {

    ForkJoinPool fjp = new ForkJoinPool();
    CountTask countTask = new CountTask(1, 4);

    Future<Integer> future = fjp.submit(countTask); // 由于需要返回结果,所以提交到线程池执行,通过future异步的得到执行结果

    int sum = future.get();
    System.out.println(sum);
  }
Exemplo n.º 16
0
 public static void main(String[] args) {
   sorted = new int[raw.length];
   RecursiveActionDemo fb = new RecursiveActionDemo(raw, 0, raw.length, sorted);
   ForkJoinPool pool = new ForkJoinPool();
   pool.invoke(fb);
   System.out.print('[');
   for (int i : sorted) {
     System.out.print(i + ",");
   }
   System.out.println(']');
 }
Exemplo n.º 17
0
 /** timed invokeAll(c) returns results of all completed tasks in c */
 public void testTimedInvokeAll5() throws Throwable {
   ForkJoinPool e = new ForkJoinPool(1);
   try (PoolCleaner cleaner = cleaner(e)) {
     List<Callable<String>> l = new ArrayList<Callable<String>>();
     l.add(new StringTask());
     l.add(new StringTask());
     List<Future<String>> futures = e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
     assertEquals(2, futures.size());
     for (Future<String> future : futures) assertSame(TEST_STRING, future.get());
   }
 }
Exemplo n.º 18
0
 /** A task submitted after shutdown is rejected */
 public void testSubmitAfterShutdown() {
   ForkJoinPool p = new ForkJoinPool(1);
   try (PoolCleaner cleaner = cleaner(p)) {
     p.shutdown();
     assertTrue(p.isShutdown());
     try {
       ForkJoinTask<Integer> f = p.submit(new FibTask(8));
       shouldThrow();
     } catch (RejectedExecutionException success) {
     }
   }
 }
Exemplo n.º 19
0
 /** Pool maintains parallelism when using ManagedBlocker */
 public void testBlockingForkJoinTask() throws Throwable {
   ForkJoinPool p = new ForkJoinPool(4);
   try {
     ReentrantLock lock = new ReentrantLock();
     ManagedLocker locker = new ManagedLocker(lock);
     ForkJoinTask<Integer> f = new LockingFibTask(20, locker, lock);
     p.execute(f);
     assertEquals(6765, (int) f.get());
   } finally {
     p.shutdownNow(); // don't wait out shutdown
   }
 }
Exemplo n.º 20
0
  /** @param args */
  public static void main(String[] args) {
    // create a random data set
    final int[] data = new int[10000000];
    final Random random = new Random();
    for (int i = 0; i < data.length; i++) {
      data[i] = random.nextInt(1000000);
    }

    // submit the task to the pool
    final ForkJoinPool pool = new ForkJoinPool(1000);
    final MaximumFinder finder = new MaximumFinder(data);
    System.out.println(pool.invoke(finder));
  }
Exemplo n.º 21
0
 public static void forkJoinSort() {
   long beginTime = System.currentTimeMillis();
   ForkJoinPool forkJoinPool = new ForkJoinPool();
   forkJoinPool.submit(new SortTask(A, 0, A.length - 1));
   forkJoinPool.shutdown();
   try {
     forkJoinPool.awaitTermination(10_000, TimeUnit.SECONDS);
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
   long endTime = System.currentTimeMillis();
   System.out.println("sort file:" + (endTime - beginTime) + "ms");
 }
Exemplo n.º 22
0
 public static void main(String[] args) {
   ForkJoinPool forkJoinPool = new ForkJoinPool();
   // 生成一个计算任务,负责计算 1+2+3+4
   CountTask task = new CountTask(1, 4);
   // 执行一个任务
   Future<Integer> result = forkJoinPool.submit(task);
   try {
     System.out.println(result.get());
   } catch (InterruptedException e) {
     e.printStackTrace();
   } catch (ExecutionException e) {
     e.printStackTrace();
   }
 }
  private byte[] getFromCache(int site) {

    int blockNumber = site / myNumLinesPerInterval;

    byte[][] result = myGenoCache.getIfPresent(blockNumber);

    if (result == null) {

      CompletableFuture<byte[]> future = new CompletableFuture<>();
      CompletableFuture<byte[]> temp = myFutureQueue.putIfAbsent(site, future);
      if (temp != null) {
        future = temp;
      }
      if (myCurrentlyProcessingBlocks.add(blockNumber)) {
        myThreadPool.submit(new ProcessLines(site));
      }

      try {
        result = myGenoCache.getIfPresent(blockNumber);
        if (result != null) {
          myFutureQueue.remove(site);
          future.complete(result[site % myNumLinesPerInterval]);
          return result[site % myNumLinesPerInterval];
        } else {
          return future.get();
        }
      } catch (Exception e) {
        myLogger.error(e.getMessage(), e);
      }
    }

    return result[site % myNumLinesPerInterval];
  }
 public static void main(String[] args) {
   repeat(
           () -> {
             try {
               BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
               System.out.print("Login: "******"Password: "******"secret");
           })
       .thenAccept(
           (a) ->
               System.out.printf(
                   "Logged in: %s %s%n", a.getUserName(), new String(a.getPassword())));
   ForkJoinPool.commonPool().awaitQuiescence(3, TimeUnit.MINUTES);
 }
Exemplo n.º 25
0
  @Override
  protected ArrayList<Resultado> compute() {

    // if work is above threshold, break tasks up into smaller tasks
    if (this.linkPagina.size() > 1) {

      List<Pagina> subtasks = new ArrayList<>();
      subtasks.addAll(createSubtasks());

      for (Pagina subtask : subtasks) {
        subtask.fork();
      }

      Resultado result;
      ArrayList<Resultado> aregloResultadoTarea;
      for (Pagina subtask : subtasks) {
        aregloResultadoTarea = subtask.join();
        result = aregloResultadoTarea.get(0);
        this.resultado.addAll(aregloResultadoTarea);
      }
      // imprimir(resultado);
      return this.resultado;

    } else {
      ArrayList<Resultado> arregloResultado = new ArrayList<>();
      Resultado resultadoTareaTexto;

      try {
        Document doc = Jsoup.connect(this.linkPagina.get(0)).get();
        int cores = Runtime.getRuntime().availableProcessors();

        String titulo = doc.title();

        Texto tareaTexto = new Texto(0, doc.body().text(), doc.body().text(), this.palabra);
        ForkJoinPool forkJoinPool = new ForkJoinPool(cores);

        resultadoTareaTexto = forkJoinPool.invoke(tareaTexto);
        // System.out.println("Titulo: "+titulo);
        resultadoTareaTexto.setTitulo(titulo);
        resultadoTareaTexto.setUrl(this.linkPagina.get(0));
        // arregloResultado.add(resultadoTareaTexto);
        arregloResultado.add(resultadoTareaTexto);
      } catch (IOException e) {
      }
      return arregloResultado;
    }
  }
Exemplo n.º 26
0
 static void test(ForkJoinPool pool, int num) throws Exception {
   int ps = pool.getParallelism();
   long start = System.nanoTime();
   DynamicFib f = new DynamicFib(num);
   pool.invoke(f);
   long time = System.nanoTime() - start;
   double secs = ((double) time) / NPS;
   long result = f.number;
   System.out.print("DynamicFib " + num + " = " + result);
   System.out.printf("\tTime: %9.3f", secs);
   long sc = pool.getStealCount();
   long ns = sc - lastStealCount;
   lastStealCount = sc;
   System.out.printf(" Steals: %4d", ns / ps);
   System.out.printf(" Workers: %4d", pool.getPoolSize());
   System.out.println();
 }
Exemplo n.º 27
0
 public static void main(String[] args) throws Exception {
   procs = 0;
   int num = 45;
   try {
     if (args.length > 0) procs = Integer.parseInt(args[0]);
     if (args.length > 1) num = Integer.parseInt(args[1]);
   } catch (Exception e) {
     System.out.println("Usage: java DynamicFib <threads> <number>");
     return;
   }
   for (int reps = 0; reps < 2; ++reps) {
     ForkJoinPool pool = (procs == 0) ? new ForkJoinPool() : new ForkJoinPool(procs);
     for (int i = 0; i < 20; ++i) test(pool, num);
     System.out.println(pool);
     pool.shutdown();
   }
 }
Exemplo n.º 28
0
 /** get of submit(callable) throws ExecutionException if callable throws exception */
 public void testSubmitEE() throws Throwable {
   ForkJoinPool p = new ForkJoinPool(1);
   try (PoolCleaner cleaner = cleaner(p)) {
     try {
       p.submit(
               new Callable() {
                 public Object call() {
                   throw new ArithmeticException();
                 }
               })
           .get();
       shouldThrow();
     } catch (ExecutionException success) {
       assertTrue(success.getCause() instanceof ArithmeticException);
     }
   }
 }
Exemplo n.º 29
0
  public static void main(String[] args) throws IOException, InterruptedException {
    long startDate = System.nanoTime();
    ForkJoinPool forkPool = new ForkJoinPool();

    File dir = new File("C:\\Documents and Settings\\akrier\\Mes documents\\Mes images");

    File[] files = dir.listFiles();

    List<File> filesResized = new LinkedList<File>();
    filesResized = (List<File>) forkPool.invoke(new ResizeTask(files));
    // tiens pas comptes des fichiers au mauvais format
    System.out.println("nb fichiers resizés :" + filesResized.size());

    long endDate = System.nanoTime();

    System.out.println(endDate - startDate);
  }
 @AsyncTimed
 public CompletableFuture<String> getMessage() {
   return CompletableFuture.supplyAsync(
       () -> {
         heavyWork();
         return "hello async world";
       },
       ForkJoinPool.commonPool());
 }