List of usage examples for java.util.concurrent Callable Callable
Callable
From source file:Main.java
private static Callable<Integer> createWaitForCallable(final Process startedProcess) { Callable<Integer> waitForCallable = new Callable<Integer>() { @Override//from w ww .j av a 2s . c o m public Integer call() throws Exception { return startedProcess.waitFor(); } }; return waitForCallable; }
From source file:net.myrrix.online.eval.ParameterOptimizer.java
public static void main(String[] args) throws Exception { if (args.length < 4) { System.err.println(/* w ww. j a v a 2s. c om*/ "Usage: dataDirectory numSteps evaluationPercentage property=min:max [property2=min2:max2 ...]"); return; } final File dataDir = new File(args[0]); Preconditions.checkArgument(dataDir.exists() && dataDir.isDirectory(), "Not a directory: %s", dataDir); Preconditions.checkArgument(dataDir.listFiles().length > 0, "No files in: %s", dataDir); int numSteps = Integer.parseInt(args[1]); Preconditions.checkArgument(numSteps >= 2, "# steps must be at least 2: %s", numSteps); final double evaluationPercentage = Double.parseDouble(args[2]); Preconditions.checkArgument(evaluationPercentage > 0.0 && evaluationPercentage <= 1.0, "evaluationPercentage must be in (0,1]: %s", evaluationPercentage); Map<String, ParameterRange> parameterRanges = Maps.newHashMapWithExpectedSize(args.length); for (int i = 3; i < args.length; i++) { String[] propValue = EQUALS.split(args[i]); String systemProperty = propValue[0]; String[] minMax = COLON.split(propValue[1]); ParameterRange range; try { int min = Integer.parseInt(minMax[0]); int max = Integer.parseInt(minMax.length == 1 ? minMax[0] : minMax[1]); range = new ParameterRange(min, max); } catch (NumberFormatException ignored) { double min = Double.parseDouble(minMax[0]); double max = Double.parseDouble(minMax.length == 1 ? minMax[0] : minMax[1]); range = new ParameterRange(min, max); } parameterRanges.put(systemProperty, range); } Callable<Number> evaluator = new Callable<Number>() { @Override public Number call() throws IOException, TasteException, InterruptedException { MyrrixIRStatistics stats = (MyrrixIRStatistics) new PrecisionRecallEvaluator().evaluate(dataDir, 0.9, evaluationPercentage, null); return stats == null ? null : stats.getMeanAveragePrecision(); } }; ParameterOptimizer optimizer = new ParameterOptimizer(parameterRanges, numSteps, evaluator, false); Map<String, Number> optimalValues = optimizer.findGoodParameterValues(); System.out.println(optimalValues); }
From source file:Main.java
public static String getDomainAddress(final String domain) { try {/* w w w . ja v a 2 s . c o m*/ ExecutorService exec = Executors.newCachedThreadPool(); Future<String> fs = exec.submit(new Callable<String>() { @Override public String call() throws Exception { InetAddress inetAddress; try { inetAddress = InetAddress.getByName(domain); return inetAddress.getHostAddress(); } catch (UnknownHostException e) { e.printStackTrace(); } return null; } }); return fs.get(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static long pmax(final long[][] arr, int numThreads) { ExecutorService pool = Executors.newFixedThreadPool(numThreads); try {/*from w w w . j a v a 2 s.c o m*/ List<Future<Long>> list = new ArrayList<Future<Long>>(); for (int i = 0; i < arr.length; i++) { final long[] subArr = arr[i]; list.add(pool.submit(new Callable<Long>() { public Long call() { long max = Long.MIN_VALUE; for (int j = 0; j < subArr.length; j++) { if (subArr[j] > max) { max = subArr[j]; } } return max; } })); } // find the max of each slice's max: long max = Long.MIN_VALUE; for (Future<Long> future : list) { long threadMax = future.get(); System.out.println("threadMax: " + threadMax); if (threadMax > max) { max = threadMax; } } return max; } catch (Exception e) { System.out.println(e); return -1; } finally { pool.shutdown(); } }
From source file:Main.java
public static Callable<Boolean> notBlank(final TextView view) { return new Callable<Boolean>() { @Override/*from w w w.j a v a 2 s . co m*/ public Boolean call() throws Exception { if (view == null || view.getText() == null) { throw new IllegalArgumentException("TextView should not be null!"); } return view.getText().length() > 0; } }; }
From source file:Main.java
public static Callable<Boolean> notEmpty(final Spinner spinner) { return new Callable<Boolean>() { @Override/*from w ww . j av a 2 s.c o m*/ public Boolean call() throws Exception { if (spinner == null) { throw new IllegalArgumentException("Spinner should not be null!"); } return spinner.getCount() > 0; } }; }
From source file:Main.java
public static <T, R> ListenableFuture<List<R>> parallelTransform(Collection<T> input, final Function<T, R> function, final ExecutorService pool) { final List<ListenableFuture<R>> futures = Lists.newLinkedList(); // make futures for (final T in : input) { ListenableFutureTask<R> task = ListenableFutureTask.create(new Callable<R>() { @Override// w ww .j a va2s . co m public R call() throws Exception { return function.apply(in); } }); pool.submit(task); futures.add(task); } return Futures.successfulAsList(futures); }
From source file:Main.java
public static void blockUntilConnected(final SocketChannel channel, long timeout) throws IOException { ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, timeout, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); FutureTask<Boolean> future = new FutureTask<Boolean>(new Callable<Boolean>() { public Boolean call() { while (!channel.isConnected()) { try { Thread.sleep(300); } catch (InterruptedException e) { }/*from w w w . ja v a 2 s. co m*/ } return true; } }); executor.execute(future); try { future.get(timeout, TimeUnit.MILLISECONDS); } catch (Exception e) { channel.close(); throw new IOException(e); } }
From source file:com.meltmedia.dropwizard.crypto.Mocks.java
public static Callable<String> mockOutput(Namespace namespace) throws UnsupportedEncodingException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); when(namespace.get(Commands.OUTFILE)).thenReturn(new PrintStream(out, true, "UTF-8")); return new Callable<String>() { @Override/*from w ww. j a va2s .c om*/ public String call() throws Exception { return out.toString("UTF-8"); } }; }
From source file:Main.java
public Main() { for (int i = 0; i < 10; i++) { final int c = i; submitTask(new Callable<Object>() { @Override//from w w w . java 2 s. co m public Object call() throws Exception { long t = System.currentTimeMillis(); try { Thread.sleep(2000); System.out.println("Task " + c + " done in " + (System.currentTimeMillis() - t) + "ms"); } catch (Exception e) { System.out .println("Task " + c + " aborted after " + (System.currentTimeMillis() - t) + "ms"); } return null; } }, 1000); } ex.shutdown(); try { ex.awaitTermination(100000, TimeUnit.MILLISECONDS); } catch (Exception e) { e.printStackTrace(); } }