private void loadBitmapAsync( String key, Resources resources, String uri, int sampleSize, FutureCallback<Bitmap> callback, Executor executor) { ListenableFutureTask<Bitmap> task; boolean isSubmitted = true; synchronized (mLock) { task = mTasks.get(key); if (task == null) { task = ListenableFutureTask.create(new LoadCallable(resources, uri, sampleSize)); mTasks.put(key, task); isSubmitted = false; } } if (!isSubmitted) { mExecutor.submit(task); } if (callback != null && executor != null) { Futures.addCallback(task, callback, executor); } }
private static void testOneSlice( final String id, final NavigableSet<Integer> test, final NavigableSet<Integer> canon, List<ListenableFuture<?>> results) { ListenableFutureTask<?> f = ListenableFutureTask.create( new Runnable() { @Override public void run() { test(id + " Count", test.size(), canon.size()); testEqual(id, test.iterator(), canon.iterator()); testEqual(id + "->DSCI", test.descendingIterator(), canon.descendingIterator()); testEqual( id + "->DSCS", test.descendingSet().iterator(), canon.descendingSet().iterator()); testEqual( id + "->DSCS->DSCI", test.descendingSet().descendingIterator(), canon.descendingSet().descendingIterator()); } }, null); results.add(f); if (DEBUG) f.run(); else COMPARE.execute(f); }
private static ListenableFutureTask<List<ListenableFuture<?>>> doOneTestInsertions( final int upperBound, final int maxRunLength, final int averageModsPerIteration, final int iterations, final boolean quickEquality) { ListenableFutureTask<List<ListenableFuture<?>>> f = ListenableFutureTask.create( new Callable<List<ListenableFuture<?>>>() { @Override public List<ListenableFuture<?>> call() { final List<ListenableFuture<?>> r = new ArrayList<>(); NavigableMap<Integer, Integer> canon = new TreeMap<>(); Object[] btree = BTree.empty(); final TreeMap<Integer, Integer> buffer = new TreeMap<>(); ThreadLocalRandom rnd = ThreadLocalRandom.current(); for (int i = 0; i < iterations; i++) { buffer.clear(); int mods = rnd.nextInt(1, averageModsPerIteration * 2); while (mods > 0) { int v = rnd.nextInt(upperBound); int rc = Math.max(0, Math.min(mods, maxRunLength) - 1); int c = 1 + (rc <= 0 ? 0 : rnd.nextInt(rc)); for (int j = 0; j < c; j++) { buffer.put(v, v); v++; } mods -= c; } Timer.Context ctxt; ctxt = TREE_TIMER.time(); canon.putAll(buffer); ctxt.stop(); ctxt = BTREE_TIMER.time(); Object[] next = null; while (next == null) next = BTree.update(btree, naturalOrder(), buffer.keySet(), SPORADIC_ABORT); btree = next; ctxt.stop(); if (!BTree.isWellFormed(btree, naturalOrder())) { log("ERROR: Not well formed"); throw new AssertionError("Not well formed!"); } if (quickEquality) testEqual("", BTree.iterator(btree), canon.keySet().iterator()); else r.addAll(testAllSlices("RND", btree, new TreeSet<>(canon.keySet()))); } return r; } }); if (DEBUG) f.run(); else MODIFY.execute(f); return f; }
@Override protected <T> PrioritizedListenableFutureTask<T> newTaskFor(Runnable runnable, T value) { Preconditions.checkArgument( allowRegularTasks || runnable instanceof PrioritizedRunnable, "task does not implement PrioritizedRunnable"); return PrioritizedListenableFutureTask.create( ListenableFutureTask.create(runnable, value), runnable instanceof PrioritizedRunnable ? ((PrioritizedRunnable) runnable).getPriority() : defaultPriority); }
@Override protected <T> PrioritizedListenableFutureTask<T> newTaskFor(Callable<T> callable) { Preconditions.checkArgument( allowRegularTasks || callable instanceof PrioritizedCallable, "task does not implement PrioritizedCallable"); return PrioritizedListenableFutureTask.create( ListenableFutureTask.create(callable), callable instanceof PrioritizedCallable ? ((PrioritizedCallable) callable).getPriority() : defaultPriority); }
public ListenableFuture<?> acquireRequestPermit() { long delayNanos = backoff.getBackoffDelayNanos(); if (delayNanos == 0) { return Futures.immediateFuture(null); } ListenableFutureTask<Object> futureTask = ListenableFutureTask.create(() -> null); scheduledExecutor.schedule(futureTask, delayNanos, NANOSECONDS); return futureTask; }
public static com.google.common.util.concurrent.ListenableFutureTask<Integer>[] populateGuavaFutureArray(int count) { Callable<Integer> func = () -> { return 0; }; com.google.common.util.concurrent.ListenableFutureTask[] array = new com.google.common.util.concurrent.ListenableFutureTask[count]; for (int i = 0; i < count; ++i) { array[i] = com.google.common.util.concurrent.ListenableFutureTask.create(func); } return array; }
public static long timeConsumingOperationOnLargeDataInThreadPool() throws ExecutionException, InterruptedException { int availableProcessorsAmount = Runtime.getRuntime().availableProcessors(); List<List<Long>> partitions = splitData(longs(1_000), availableProcessorsAmount); ExecutorService executorService = Executors.newWorkStealingPool(); List<ListenableFuture<Long>> listenableFutures = new ArrayList<>(); for (List<Long> partition : partitions) { ListenableFutureTask<Long> e = ListenableFutureTask.create(() -> compute(partition)); executorService.submit(e); listenableFutures.add(e); } List<Long> longs = Futures.allAsList(listenableFutures).get(); return sum(longs); }
public ListenableFuture a(Callable callable) { Validate.notNull(callable); if (!this.isMainThread()) { ListenableFutureTask listenablefuturetask = ListenableFutureTask.create(callable); Queue queue = this.i; synchronized (this.i) { this.i.add(listenablefuturetask); return listenablefuturetask; } } else { try { return Futures.immediateFuture(callable.call()); } catch (Exception exception) { return Futures.immediateFailedCheckedFuture(exception); } } }
public ListenableFuture callFromMainThread(Callable callable) { Validate.notNull(callable); if (!this.isCallingFromMinecraftThread()) { ListenableFutureTask var2 = ListenableFutureTask.create(callable); Queue var3 = this.futureTaskQueue; synchronized (this.futureTaskQueue) { this.futureTaskQueue.add(var2); return var2; } } else { try { return Futures.immediateFuture(callable.call()); } catch (Exception var6) { return Futures.immediateFailedCheckedFuture(var6); } } }
protected final ListenableFutureTask newTaskFor(Callable callable) { return ListenableFutureTask.create(callable); }
protected final ListenableFutureTask newTaskFor(Runnable runnable, Object obj) { return ListenableFutureTask.create(runnable, obj); }
public static <V> PrioritizedListenableFutureTask<?> create(PrioritizedCallable<V> callable) { return new PrioritizedListenableFutureTask<>( ListenableFutureTask.create(callable), callable.getPriority()); }
public static <V> PrioritizedListenableFutureTask<V> create( PrioritizedRunnable task, @Nullable V result) { return new PrioritizedListenableFutureTask<>( ListenableFutureTask.create(task, result), task.getPriority()); }