Refine search
return super.waitFinished(timeout); } else { return true; return super.waitFinished(timeout);
/** Delegates valid cancel requests to asociated AsyncGUIJob, in the case * job supports cancelling. */ private void cancel() { if ((initTask != null) && !initTask.isFinished() && (initJob instanceof Cancellable)) { synchronized (CANCELLED_LOCK) { LOG.log(Level.FINE, "Cancelling for {0}", comp4Init); wasCancelled = true; } ((Cancellable) initJob).cancel(); LOG.fine("Cancelling done"); } }
ExecutorService executor = Executors.newSingleThreadExecutor(); executor.invokeAll(Arrays.asList(new Task()), 10, TimeUnit.MINUTES); // Timeout of 10 minutes. executor.shutdown();
/** Start the task. * When it finishes (even with an exception) it calls * {@link #notifyFinished}. * Subclasses may override this method, but they * then need to call {@link #notifyFinished} explicitly. * <p>Note that this call runs synchronously, but typically the creator * of the task will call this method in a separate thread. */ public void run() { try { notifyRunning(); if (run != null) { run.run(); } } finally { notifyFinished(); } }
ExecutorService es = Executors.newFixedThreadPool(3); for(int i=0;i<10;i++) es.submit(new Task(i));
ExecutorService executor = Executors.newSingleThreadExecutor(); Future<Integer> future = executor.submit(new Task()); try { System.out.println("Started"); Integer retval = future.get(10, TimeUnit.SECONDS)); // you can choose your desired time here System.out.println("Finished"); } catch (TimeoutException e) { future.cancel(true); System.out.println("Timeout happened"); // handle termination here } executor.shutdownNow();
public class MyTask implements Runnable { @Override public void run() { ... } } ExecutorService s = Executors.newSingleThreadExecutor(); Future<?> submit = s.submit(new Task());
ExecutorService exec = Executors.newCachedThreadPool(); for(int i = 0; i < LARGE_NUMBER; i++) { exec.submit(new Task(i)); } exec.awaitTermination(1000, TimeUnit.HOURS); // wait for all tasks to finish
ExecutorService executor = Executors.newFixedThreadPool(tasks.size()); for (final Task task : tasks) { executor.submit(new Runnable() { @Override public void run() { task.run(args); } }); } executor.shutdown(); executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLSECONDS);
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); List<Future<String>> results = executor.invokeAll(Arrays.asList(new Task()), 1, TimeUnit.SECONDS); // Timeout of 1 second. for (Future<String> result : results) { if (!result.isCancelled()) { System.out.println(result.get()); // Won't be printed as the "actual" processing took 2 seconds. } else { System.out.println("Task timed out."); } } executor.shutdown();
@WebListener public class Config implements ServletContextListener { private ScheduledExecutorService scheduler; @Override public void contextInitialized(ServletContextEvent event) { scheduler = Executors.newSingleThreadScheduledExecutor(); scheduler.scheduleAtFixedRate(new Task(), 0, 1, TimeUnit.MINUTES); // Schedule to run every minute. } @Override public void contextDestroyed(ServletContextEvent event) { scheduler.shutdown(); // Important! This stops the thread. } }
ExecutorService service = Executors.newCachedThreadPool(); // My task Task task = new Task(); // Submit the task and get the corresponding future Future<?> future = service.submit(task); ... // Cancel the task which will interrupt the thread that was executing the // task if any future.cancel(true);
ExecutorService executor = Executors.newFixedThreadPool(2); CompletionService<String> compService = new ExecutorCompletionService<>(executor); for (int i = 0; i < 2; i++) { compService.submit(new Task(i)); } for (int i = 0; i < 2; i++) { try { String result = compService.take().get(); System.out.println("RESULT: " + result); } catch (ExecutionException | InterruptedException e) { e.printStackTrace(); } } executor.shutdownNow();
// "Wrapped" (and thus shared) by task0 and task1 below. Runnable runnable = new Runnable() { @Override public void run() { System.out.println("task was invoked"); } } Timer timer0 = new Timer(); Task task0 = new Task(runnable); timer0.schedule(task0, delay); timer0.cancel(); Task task1 = new Task(runnable); Timer timer1 = new Timer(); timer1.schedule(task1, delay); // throws an exception if we use task0 Thread.sleep(5000); timer1.cancel();
ExecutorService executor = Executors.newFixedThreadPool(3); CompletionService<Void> compService = new ExecutorCompletionService<Void>(executor); for(int i = 0; i < 3; i++) compService.submit(new Task(), null); while(true) { compService.take(); // wait until one of the threads finishes its task compService.submit(new Task(), null); }
final ExecutorService executorService = Executors.newCachedThreadPool(); final ExecutorCompletionService<String> completionService = new ExecutorCompletionService<String>(executorService); for (int i = 0; i < 10; ++i) { completionService.submit(new Task()); } completionService.take().get();