public static void main(String args[]) throws Exception { ExecutorService pool = Executors.newFixedThreadPool(3); Set<Future<Integer>> set = new HashSet<Future<Integer>>(); for (String word: args) { Callable<Integer> callable = new WordLengthCallable(word); Future<Integer> future = pool.submit(callable); set.add(future); } int sum = 0; for (Future<Integer> future : set) { sum += future.get(); } System.out.printf("The sum of lengths is %s%n", sum); System.exit(sum); }
Callable<V> costlyIdempotentOperation = new Callable<>() {...}; // this would block only the thread to execute the callable ListenableFuture<V> future = new OnceEnteredCallable<>().runOnce(costlyIdempotentOperation); // this would block all the threads and set the reference fCachedValue.set(future.get()); // this would set the reference upon computation, Java 8 syntax future.addListener(() -> {fCachedValue.set(future.get())}, executorService);
Callable<Boolean> testCollumnsTask = new Callable<>(){ @Override public Boolean call(){ return Boolean.valueOf(testCols(board)); } }
Callable<Thing> callable = new Getter(); Future<Thing> future = exec.submit(callable); doSomethingElseThatTakesAWhile(); return future.get();
Callable<Thing> callable = new Getter(); Future<Thing> future = exec.submit(callable); return future.get();
Callable wifiStateCallback = new MyCallable(false);
public void myServerInitiator() throws IOException { server = HttpServer.create(new InetSocketAddress(port), 0); hc = server.createContext("/test", new MyHandler()); // create all the thread and start it here for(int i =0; i<5;i++) Callable<Boolean> callable = new MyDelegator (); Future<Boolean> future = pool.submit(callable); set.add(future); } ........ }
public abstract class Worker { protected abstract Object doTheWork(); public Future<Object> update() { Exception stack = new Exception(); Callable<Object> job = new WhateverYourCallableIs(stack); return submitJob(job); } }
ExecutorService service = ... some ExecutorService...; Callable<Integer> myCallable = new MyCallable( ... some parameters ... ); Future<Integer> future = service.submit( myCallable ); ... Integer myResult = future.get(); // will wait and return return value from callable as soon as done
public static void main(String[] args) { ExecutorService es = Executors.newFixedThreadPool(10); Callable<Integer> callable1 = new CallableImpl(); Future<Integer> future1 = es.submit(callable1); // if you decide to cancel your task gracefully future1.cancel() ...
BlockingQueue<Runnable> workQ = new LinkedBlockingQueue<Runnable>(); ExecutorService execService = new ThreadPoolExecutor(4, 4, 0L, TimeUnit.SECONDS, workQ); ... execService.submit(new Callable(...));
ExecutorService executor = Executors.newFixedThreadPool(10); List<Future<String>> list = new ArrayList<Future<String>>(); Callable<String> callable = new MyCallable(); for (int i = 0; i < 100; i++) { Future<String> future = executor.submit(callable); list.add(future); }
Callable<Integer> callable = new CallableImpl(2); ExecutorService executor = new ScheduledThreadPoolExecutor(5); Future<Integer> future = executor.submit(callable); Integer result = future.get(600,TimeUnit.seconds));
ExecutorService service=Executors.newFixedThreadPool(1); Callable<ResultSet> callable = new SelectThreadResultSet(); Future<ResultSet> future = service.submit(callable); ResultSet rs = (ResultSet)future.get(); // Here I dont get any values while(rs.next()){ System.out.println(rs.getString("EMPLOYEE")); }
ExecutorService executor = Executors.newSingleThreadExecutor(); // Create vanilla executor service. CompletionService<Result> completionService = new ExecutorCompletionService<Result>(executor); // Completion service wraps executor and is notified of results as they complete. Callable<Result> callable = new MyCallable(); executor.submit(callable); // Do not store handle to Future here but rather obtain from CompletionService when we *know* the result is complete. Future<Result> fut = completionService.take(); // Will block until a completed result is available. Result result = fut.get(); // Will not block as we know this future represents a completed result.
List<Future<Double>> futures = new ArrayList<>(); for (int i = 0; i < 10; i++) { double p1 = Math.pow(2, i); for (int j = 5; j < 15; j++) { double p2 = Math.pow(2, j); Callable<Double> worker = new ProbabilityCalculator(p1, p2); futures.add(executor.submit(worker)); } } // Do whatever else you want to here... // Now wait for the results and handle them appropriately. // Note the exceptions that can be thrown... for (Future<Double> future : futures) { double value = future.get(); ... }
// Initiate you thread pool and make sure it is unique ExecutorService service = Executors.newFixedThreadPool(1); ... // In your web method: Future<?> futureResult = service.submit(new Runnable()/or new Callable()); // Using callable, you will get a Typed Future Object result = futureResult.get();// If you need to wait for the result of the runnable/callable. ...
public class Utils { public static String selectQuery = "SELECT EMPNAME FROM EMPLOYEEDETAILS"; public static void dbSelect(String query) throws InterruptedException, ExecutionException, SQLException{ ExecutorService service=Executors.newFixedThreadPool(1); Callable<List<String>> callable = new SelectThreadClass(); Future<List<String>> future = service.submit(callable); List<String> empList = future.get(); for(String emp : empList){ System.out.println(emp); } } public static void main(String args[]) throws InterruptedException, ExecutionException, SQLException{ dbSelect("SELECT EMPNAME FROM EMPLOYEEDETAILS"); } }
final DataFetcher fetcher = new DataFetcher(); Callable<Data> task1 = new Callable<>() { @Override public Data call() { return fetcher.fetchData(1, 2); } }; Callable<Data> task2 = new Callable<>() { @Override public Data call() { return fetcher.fetchData(3, 4); } }; Future<Data> result1 = executorService.submit(task1); Future<Data> result2 = executorService.submit(task2);