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(); } } }
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]; }
/** 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()); } }
/** * 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); } }
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(); }
@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(); } }
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); } }
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); }
/** 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) { } } }
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"); }
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(); } }
/** 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); } } }
/** getPoolSize returns number of started workers. */ public void testGetPoolSize() { final CountDownLatch taskStarted = new CountDownLatch(1); final CountDownLatch done = new CountDownLatch(1); final ForkJoinPool p = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(p)) { assertEquals(0, p.getActiveThreadCount()); final Runnable task = new CheckedRunnable() { public void realRun() throws InterruptedException { taskStarted.countDown(); assertEquals(1, p.getPoolSize()); assertEquals(1, p.getActiveThreadCount()); done.await(); } }; Future<?> future = p.submit(task); await(taskStarted); assertEquals(1, p.getPoolSize()); assertEquals(1, p.getActiveThreadCount()); done.countDown(); } assertEquals(0, p.getPoolSize()); assertEquals(0, p.getActiveThreadCount()); }
public static void downloadEncryptedAllVersions( List<NUSTitleInformation> output_, final Progress progress) { ForkJoinPool pool = new ForkJoinPool(25); List<ForkJoinTask<Boolean>> list = new ArrayList<>(); final int outputsize = output_.size(); for (final NUSTitleInformation nus : output_) { final long tID = nus.getTitleID(); list.add( pool.submit( new Callable<Boolean>() { @Override public Boolean call() throws Exception { int count = 1; for (Integer i : nus.getAllVersions()) { NUSTitle nusa = new NUSTitle(tID, i, Util.ByteArrayToString(nus.getKey())); Progress childProgress = new Progress(); progress.add(childProgress); nusa.downloadEncryptedFiles(progress); System.out.println( "Update download progress " + "(" + nus.getLongnameEN() + ") version " + i + " complete! This was " + count + " of " + nus.getAllVersions().size() + "!"); count++; } System.out.println( "Update download complete " + "(" + nus.getLongnameEN() + ")" + "! Loaded updates for " + nus.getAllVersions().size() + " version. Now are " + finished.incrementAndGet() + " of " + outputsize + " done! "); 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(); } } }
/** * Sorts all the elements of the given array using the ForkJoin framework * * @param array the array to sort */ public void sort(int[] array) { ForkJoinTask<Void> job = pool.submit(new MergeSortTask(array, 0, array.length)); job.join(); }