futuretask

Getting a result in the future?

痴心易碎 提交于 2021-02-07 05:58:07
问题 I'm looking to get a result from a method which can take a while to complete and doesn't actually return the object, so I'd like to deal with it as effectively as possible. Here's an example of what I'm trying to achieve: public static void main (String[] args) { Object obj = someMethod(); System.out.println("The object is" + obj + ", wooh!"); } public void callObject() { // Sends request for the object } public void receiveObject(Object object) { // Received the object } public Object

Constructing a DAG of cancelable Java tasks

孤人 提交于 2020-12-15 05:25:32
问题 I want to create a DAG out of tasks in Java, where the tasks may depend upon the output of other tasks. If there is no directed path between two tasks, they may run in parallel. Tasks may be canceled. If any task throws an exception, all tasks are canceled. I wanted to use CompleteableFuture for this, but despite implementing the Future interface (including Future.cancel(boolean) , CompletableFuture does not support cancelation -- CompletableFuture.cancel(true) is simply ignored. (Does

How to stop a FixedRateScheduledTask in Spring without interrupting a running thread

☆樱花仙子☆ 提交于 2020-05-17 07:07:19
问题 In my SpringBoot application, I have a FixedRateScheduledTask. I want to dynamically change the fixed interval upon a refresh event. Note that depending on the processing time, there could be several parallel threads, thanks to Async. Upon a refresh event, I cancel ed the current ScheduledTask and re-added a new FixedRateTask with new value for interval . Below is my code @Configuration @EnableAsync @EnableScheduling public class Scheduler implements SchedulingConfigurer, ApplicationListener

How to stop a FixedRateScheduledTask in Spring without interrupting a running thread

梦想与她 提交于 2020-05-17 07:07:09
问题 In my SpringBoot application, I have a FixedRateScheduledTask. I want to dynamically change the fixed interval upon a refresh event. Note that depending on the processing time, there could be several parallel threads, thanks to Async. Upon a refresh event, I cancel ed the current ScheduledTask and re-added a new FixedRateTask with new value for interval . Below is my code @Configuration @EnableAsync @EnableScheduling public class Scheduler implements SchedulingConfigurer, ApplicationListener

CompletionService小记

风流意气都作罢 提交于 2020-03-01 04:06:18
CompletionService小记 在使用ExecutorService时,通过sumit执行一个Callable的时候,会立即返回一个异步任务结果,然后通过get获取异步任务结果的时候会阻塞,如下面这种情况,代码如下, import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; /** * Created by xinxingegeya on 16/3/22. */ public class ExecutorServiceTest { public static void main(String args[]) throws ExecutionException, InterruptedException { ExecutorService

java并发callable,runnable,future和futureTask

假如想象 提交于 2020-02-28 21:10:10
java多线程 java实现多线程有两种方式:一个是直接继承Thread类,一种是实现Runnable接口。但这2种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果。于是后面又增加了Callable和Future,通过它们可以在任务执行完毕之后得到任务执行结果。 callable与runnable java.lang.Runnable是一个接口,在它里面只声明了一个run()方法: public interface Runnable { public abstract void run(); } 由于run()方法返回值为void类型,所以在执行完任务之后无法返回任何结果。   Callable位于java.util.concurrent包下,它也是一个接口,在它里面也只声明了一个方法,只不过这个方法叫做call(): public interface Callable<V> { /** * Computes a result, or throws an exception if unable to do so. * * @return computed result * @throws Exception if unable to compute a result */ V call() throws Exception; } 可以看到,这是一个泛型接口,call(

How FutureTask is asynchronous computation

倖福魔咒の 提交于 2020-01-14 07:50:08
问题 new Thread(new Runnable() { public void run() { ............. ............. ............. } }).start(); If i will do this in main it will create a new thread and will submit a task to it for asynchronous calculation. If you see FutureTask documentation it also says: A cancellable asynchronous computation. This class provides a base implementation of Future, with methods to start and cancel a computation, query to see if the computation is complete, and retrieve the result of the computation.

java Callable FutureTask Excecuter: How to listen to finished task

瘦欲@ 提交于 2019-12-30 03:19:11
问题 I'm quite new to executer services. Liked doing everything myself, but I think it's time to trust these services. I want to hand by Executer a Runnable . The executer wraps that in a FutureTask and hands it back to me. Now I call poll the done() method. But I would like to be notified when then done() method would return true. There is a get() method that blocks until the Runnable has finished, but then I would need a extra thread for every job, just to see when it's finished. Can I hand my

ScheduledExecutorService.scheduleAtFixedRate And Setting initialDelay To Date In The Past

眉间皱痕 提交于 2019-12-23 14:03:14
问题 I'm working on a scheduling system in Java that sends out reminders based on a startDate , endDate and occurrence (hourly, daily, weekly, monthly, Mondays, etc). Originally I was using Timer and TimerTask classes to schedule the reminders: Timer timer = new Timer(); timer.scheduleAtFixedRate(reminder, firstDate, period); I recently switched to ScheduledExecutorService so I could have more control on cancelling events. The ScheduledExecutorService is working well for recurring reminders,

ScheduledExecutorService.scheduleAtFixedRate And Setting initialDelay To Date In The Past

戏子无情 提交于 2019-12-23 14:02:55
问题 I'm working on a scheduling system in Java that sends out reminders based on a startDate , endDate and occurrence (hourly, daily, weekly, monthly, Mondays, etc). Originally I was using Timer and TimerTask classes to schedule the reminders: Timer timer = new Timer(); timer.scheduleAtFixedRate(reminder, firstDate, period); I recently switched to ScheduledExecutorService so I could have more control on cancelling events. The ScheduledExecutorService is working well for recurring reminders,