List of utility methods to do Thread Callable
R | runWithTimeout(long millisTimeout, Callable Runs and blocking waits for the given callable to finish for the given time. ExecutorService singleThreadExecutor = Executors.newFixedThreadPool(1); Future<R> future = singleThreadExecutor.submit(callable); try { return future.get(millisTimeout, TimeUnit.MILLISECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e) { } finally { singleThreadExecutor.shutdown(); return null; |
Callable | shuffleRow(final double[] data) shuffle Row return new Callable() { int len = data.length; public Object call() throws Exception { for (int i = 0; i < data.length; i++) { double temp = data[i]; int index = random.nextInt(len); data[i] = data[index]; data[index] = temp; ... |
T | submitAndWait(ListeningExecutorService service, Callable submit And Wait ListenableFuture<T> lf = service.submit(ca);
return lf.get();
|
List | submitManyAndWait(ListeningExecutorService service, Iterable submit Many And Wait List<ListenableFuture<T>> lfs = new ArrayList<>(); for (Callable<T> ca : cas) { lfs.add(service.submit(ca)); return Futures.successfulAsList(lfs).get(); |
Callable | sum(final double[] a) sum Callable c = new Callable() { public Object call() throws Exception { Double max = null; double[] result = new double[a.length]; max = a[0]; for (int i = 1; i < a.length; i++) { max += a[i]; return max; }; return c; |
Callable | sumOfSquares(final double[] data) sum Of Squares return new Callable() { int len = data.length; double sum = 0; public Object call() throws Exception { for (int i = 0; i < data.length; i++) { double temp = data[i]; sum += temp * temp; return sum; }; |
Callable | waitFor(final Process process, final CyclicBarrier onStart) Waits for a process to terminate. return new Callable<Integer>() { public Integer call() throws InterruptedException, BrokenBarrierException { onStart.await(); return process.waitFor(); }; |
void | waitFor(Object o, long ms, Callable Wait for async work to finish. synchronized (o) { final long before = System.currentTimeMillis(); final long step = ms / 10; long d = ms; while (!c.call() && (0 < d)) { o.wait(0 < step ? step : 1); d = ms - (System.currentTimeMillis() - before); |
void | waitForCondition(Callable wait For Condition try { while (seconds-- > 0) { if (condition.call()) { return; if (callback != null) { callback.call(); Thread.sleep(500); } catch (InterruptedException ee) { ee.printStackTrace(); throw ee; } catch (Exception ee) { throw ee; throw new Exception("condition not met in time!"); |