private Task<?> addResponse(final Promise<Integer> response) { return Task.action("addResponse", new Action() { @Override public void run() { _responses.add(response.get()); } }); } }
private Task<?> networkClassifyTask(final Task<Network> network) { return Task.action("NetworkClassifier", new Action() { @Override public void run() { doClassify(new NetworkClassifier(network.get())); } }); }
private Task<?> truthMapClassifyTask(final String name, final Classification classification, final Promise<Map<Long, Boolean>> result) { return Task.action(name + "Classifier", new Action() { @Override public void run() { doClassify(new TruthMapClassifier(classification, result.get())); } }); }
private Task<?> connectedClassifyTask(final Task<Network> network) { return Task.action("ConnectedClassifier", new Action() { @Override public void run() { doClassify(new ConnectedClassifier(network.get())); } }); }
private Task<?> checkDone() { return Task.action("checkDone", new Action() { @Override public void run() { final int index = Math.min(WAIT_TIMES.length - 1, _responses.size()); if (WAIT_TIMES[index] + _startMillis <= System.currentTimeMillis()) { _result.done(_responses); } } }); }
private Task<?> classifyTask(final Classifier classifier) { return Task.action(classifier.getClass().getSimpleName(), new Action() { @Override public void run() { doClassify(classifier); } }); }
/** * Creates a new {@link Task} that have a value of type Void. Because the * returned task has no value, it is typically used to produce side-effects. * * @deprecated As of 2.0.0, replaced by {@link Task#action(String, com.linkedin.parseq.function.Action) Task.action} * @param name a name that describes the action * @param runnable the action that will be executed when the task is run * @return the new task * @see Task#action(String, com.linkedin.parseq.function.Action) Task.action */ @Deprecated public static Task<Void> action(final String name, final Runnable runnable) { return Task.action(name, runnable::run); }
/** * Equivalent to {@code action("action", action)}. * @see #action(String, Action) */ public static Task<Void> action(final Action action) { return action("action: " + _taskDescriptor.getDescription(action.getClass().getName()), action); }
public static Task<?> noop() { return Task.action("noop", () -> { } ); }
private static <T> Task<?> enqueueTask(final Queue<T> queue, final T value) { return Task.action("enqueue", () -> queue.add(value)); } }
@Test public void testAction() { final AtomicReference<String> variable = new AtomicReference<String>(); Task<Void> task = Task.action(() -> variable.set("value")); runAndWait("TestTaskFactoryMethods.testAction", task); assertEquals(variable.get(), "value"); assertEquals(countTasks(task.getTrace()), 1); }
@Test public void testRunWithinCapacity() throws InterruptedException { final AtomicInteger counter = new AtomicInteger(0); Task<?>[] tasks = new Task<?>[10]; for (int i = 0; i < 10; i++) { tasks[i] = Task.action(counter::incrementAndGet); } for (int i = 0; i < 10; i++) { _engine.run(tasks[i]); } assertTrue(awaitAll(tasks)); assertEquals(10, counter.get()); }
@Test public void testRunWithinCapacity() throws InterruptedException { final AtomicInteger counter = new AtomicInteger(0); Task<?>[] tasks = new Task<?>[10]; for (int i = 0; i < 10; i++) { tasks[i] = Task.action(counter::incrementAndGet); } for (int i = 0; i < 10; i++) { _engine.run(tasks[i], (i % 2 == 0) ? "evenPlan" : "oddPlan"); } assertTrue(awaitAll(tasks)); assertEquals(10, counter.get()); }
@Test public void testBlockingRunWithinCapacity() throws InterruptedException { final AtomicInteger counter = new AtomicInteger(0); Task<?>[] tasks = new Task<?>[10]; for (int i = 0; i < 10; i++) { tasks[i] = Task.action(counter::incrementAndGet); } for (int i = 0; i < 10; i++) { _engine.blockingRun(tasks[i], (i % 2 == 0) ? "evenPlan" : "oddPlan"); } assertTrue(awaitAll(tasks)); assertEquals(10, counter.get()); }
@Test public void testBlockingRunWithinCapacity() throws InterruptedException { final AtomicInteger counter = new AtomicInteger(0); Task<?>[] tasks = new Task<?>[10]; for (int i = 0; i < 10; i++) { tasks[i] = Task.action(counter::incrementAndGet); } for (int i = 0; i < 10; i++) { _engine.blockingRun(tasks[i]); } assertTrue(awaitAll(tasks)); assertEquals(10, counter.get()); }
@Test public void testTimeBoundedTryRunWithinCapacity() throws InterruptedException { final AtomicInteger counter = new AtomicInteger(0); Task<?>[] tasks = new Task<?>[10]; for (int i = 0; i < 10; i++) { tasks[i] = Task.action(counter::incrementAndGet); } for (int i = 0; i < 10; i++) { assertTrue(_engine.tryRun(tasks[i], 10, TimeUnit.MILLISECONDS)); } assertTrue(awaitAll(tasks)); assertEquals(10, counter.get()); }
@Test public void testTryRunWithinCapacity() throws InterruptedException { final AtomicInteger counter = new AtomicInteger(0); Task<?>[] tasks = new Task<?>[10]; for (int i = 0; i < 10; i++) { tasks[i] = Task.action(counter::incrementAndGet); } for (int i = 0; i < 10; i++) { assertTrue(_engine.tryRun(tasks[i])); } assertTrue(awaitAll(tasks)); assertEquals(10, counter.get()); }
@Test public void testTryRunWithinCapacity() throws InterruptedException { final AtomicInteger counter = new AtomicInteger(0); Task<?>[] tasks = new Task<?>[10]; for (int i = 0; i < 10; i++) { tasks[i] = Task.action(counter::incrementAndGet); } for (int i = 0; i < 10; i++) { assertTrue(_engine.tryRun(tasks[i], (i % 2 == 0) ? "evenPlan" : "oddPlan")); } assertTrue(awaitAll(tasks)); assertEquals(10, counter.get()); }
@Test public void testTimeBoundedTryRunWithinCapacity() throws InterruptedException { final AtomicInteger counter = new AtomicInteger(0); Task<?>[] tasks = new Task<?>[10]; for (int i = 0; i < 10; i++) { tasks[i] = Task.action(counter::incrementAndGet); } for (int i = 0; i < 10; i++) { assertTrue(_engine.tryRun(tasks[i], (i % 2 == 0) ? "evenPlan" : "oddPlan", 10, TimeUnit.MILLISECONDS)); } assertTrue(awaitAll(tasks)); assertEquals(10, counter.get()); }
@Override public Promise<Queue<Integer>> run(final Context context) throws Exception { final SettablePromise<Queue<Integer>> promise = Promises.settable(); final Queue<Integer> queue = new LinkedList<Integer>(); final Task<?> t1 = enqueueTask(queue, 1); t1.setPriority(-5); final Task<?> t2 = enqueueTask(queue, 2); t2.setPriority(10); final Task<?> t3 = enqueueTask(queue, 3); t3.setPriority(0); context.run(t1, t2, t3); context.after(t1, t2, t3).run(Task.action("done", () -> promise.done(queue))); return promise; } };