future

How to properly deal with exceptions coming from ListenableFuture guava?

a 夏天 提交于 2019-12-01 23:14:22
I have a library in which I have provided two methods, sync and async for our customer. They can call whichever method they feel is right for their purpose. executeSynchronous() - waits until I have a result, returns the result. executeAsynchronous() - returns a Future immediately which can be processed after other things are done, if needed. They will pass DataKey object which has the user id in it. And we will figure out which machine to call basis on the user id. So we will make http call to the url using AsyncRestTemplate and then send the response back to them basis on whether it is

Future

久未见 提交于 2019-12-01 23:03:31
/** * Future 未来的执行结果 */ public class T06_Future { public static void main(String[] args) throws ExecutionException, InterruptedException { // 未来任务, 既是Runnable 也是 Future FutureTask<Integer> task = new FutureTask<>(() -> { TimeUnit.MILLISECONDS.sleep(500); return 100; }); new Thread(task).start(); System.out.println(task.get()); // 阻塞等待任务执行完成, 获取到返回值 System.out.println("-------------------------------"); //******************************** // 使用ExecutorService的submit替代FutureTask ExecutorService service = Executors.newFixedThreadPool(5); Future<Integer> result = service.submit(() -> { TimeUnit

Calling a shiny JavaScript Callback from within a future

若如初见. 提交于 2019-12-01 17:18:27
问题 In shiny, it is possible to call client-side callbacks written in javascript from the server's logic. Say in ui.R you have some JavaScript including a function called setText : tags$script(' Shiny.addCustomMessageHandler("setText", function(text) { document.getElementById("output").innerHTML = text; }) ') then in your server.R you can call session$sendCustomMessage(type='foo', 'foo') . Suppose I have a long-running function which returns some data to plot. If I do this normally, the R thread

Unable to render “loading” using Shiny and futures

南笙酒味 提交于 2019-12-01 14:01:24
I am trying to use futures to have a "loading" icon appear. This is the code I have library(shiny) library(promises) library(future) plan(multiprocess) disksUI <- function(id) { ns <- NS(id) fluidRow( box( uiOutput(ns("loading")), dataTableOutput(ns("filelist")), width=12 ) ) } disksServer <- function(input, output, session) { state <- reactiveValues(onLoading=FALSE) observe({ if (state$onLoading) { output$loading <- renderUI("Loading") } else { output$loading <- renderUI("Done") } }) filelist <- reactive( { state$onLoading <- TRUE future({ Sys.sleep(3) state$onLoading <- FALSE } ) } ) output

How to kill CompletableFuture related threads?

*爱你&永不变心* 提交于 2019-12-01 12:32:52
问题 I have method that is checking the CompletableFuture execution time. If such CompletableFuture is executing for more than 2 seconds i want to kill this task. But how can I doit if i don't have control overy thread where CompletableFuture methods are executed ? final CompletableFuture<List<List<Student>>> responseFuture = new CompletableFuture<>(); responseFuture.supplyAsync(this::createAllRandomGroups) .thenAccept(this::printGroups) .exceptionally(throwable -> { throwable.printStackTrace();

The Future is not complete?

筅森魡賤 提交于 2019-12-01 09:14:15
问题 object Executor extends App { implicit val system = ActorSystem() implicit val materializer = ActorMaterializer() implicit val ec = system.dispatcher import akka.stream.io._ val file = new File("res/AdviceAnimals.tsv") import akka.stream.io.Implicits._ val foreach: Future[Long] = SynchronousFileSource(file) .to( Sink.outputStream(()=>System.out)) .run() foreach onComplete { v => println(s"the foreach is ${v.get}") // the will not be print } } but if I change the Sink.outputStream(()=>System

Is there sequential Future.find?

荒凉一梦 提交于 2019-12-01 09:13:06
I have some side-effectful function, def f(): Future[Int] = { val n = Random.nextInt() println(s"Generated $n") Future(n) } and I want to execute it repeatedly until predicate returns true. def success(n: Int): Boolean = n % 2 == 0 My plan is to build Stream of results val s = Stream.fill(10)(f) and then use Future.find to get the first result that satisfies predicate. Future.find(s)(success) map println The problem is that Future.find runs all the futures in parallel and I want it to execute futures sequentially one after the other until predicate returns true. scala> Future.find(s)(success)

How to use the Guava ListenableFuture and the Futures.chain() methods

五迷三道 提交于 2019-12-01 09:10:40
I have a homework task that requires me to learn how to use the Guava concurrency library. In the task I have several thread pools, where each one is controlled by an individual object. Each pool has several working threads that perform simple tasks (mostly emulating doing stuff by using Thread.sleep(long) ) and all those simple tasks are stored in a container object that emulates a messageboard. Each simple task has a dependency list of other tasks, and it cannot be executed until all of those tasks are completed. How can I benefit from the Guava library using the ListenableFuture and the

Is there sequential Future.find?

徘徊边缘 提交于 2019-12-01 06:47:16
问题 I have some side-effectful function, def f(): Future[Int] = { val n = Random.nextInt() println(s"Generated $n") Future(n) } and I want to execute it repeatedly until predicate returns true. def success(n: Int): Boolean = n % 2 == 0 My plan is to build Stream of results val s = Stream.fill(10)(f) and then use Future.find to get the first result that satisfies predicate. Future.find(s)(success) map println The problem is that Future.find runs all the futures in parallel and I want it to execute