future

Why is there no default java implementation of Delayed for DelayQueue?

时光总嘲笑我的痴心妄想 提交于 2020-02-06 03:52:28
问题 I'd like a DelayQueue of scheduled Runnable s, where each Runnable s should only be run after a certain point in time, specified beforehand. Hence a thread can just keep removing runnables from this queue and process a schedule of events. Why is there no good default implementation of Delayed, that is also Runnable , for this? The only subinterface of Delayed that seems reasonable is RunnableScheduledFuture, which has a whole bunch of random things that need to be implemented. There has to be

Future和Callable

荒凉一梦 提交于 2020-02-03 08:04:43
1 Runnable 的 缺陷 不能返回一个 返回值 也不能 抛出 checked Exception 为什么 有这样的缺陷? Runnable为什么 设计 成这样? 针对于无法抛岀检査后异常这个缺陷的 补救措施 @FunctionalInterface public interface Runnable { public abstract void run(); } /** * 在run方法中无法抛出checked Exception */ public class RunnableCantThrowsException { public void ddd() throws Exception { throw new Exception(); } public static void main(String[] args) { Runnable runnable = new Runnable() { @Override public void run() { //throw new Exception(); try { throw new Exception(); } catch (Exception e) { e.printStackTrace(); } } }; } } 2 Callable 接口 类似于 Runnable ,被其它线程执行的任务 实现 call 方法

How do I wrap a blocking function into a promise RShiny

余生长醉 提交于 2020-01-30 09:41:27
问题 I have an R Shiny dashboard that has 2 observers that are set to refresh at specific times, One observer refreshes every 6 hours, the other every 2 mins. Both observers run a function that returns a reactive value. This works fine, however every 6 hours when the first observer is triggered it locks the dashboard and prevents the other observer from functioning. After some reading I know that I need to use futures and promises but am unable to implement anything that works as intended. How do

How to ask CompletableFuture use non-daemon threads?

馋奶兔 提交于 2020-01-29 08:43:06
问题 I have wrote following code: System.out.println("Main thread:" + Thread.currentThread().getId()); CompletableFuture<Void> future = CompletableFuture.runAsync(() -> { try { System.out.println("Before sleep thread:" + Thread.currentThread().getId(), + " isDaemon:" + Thread.currentThread().isDaemon()); Thread.sleep(100); System.out.println("After sleep"); } catch (InterruptedException e) { e.printStackTrace(); } }); future.whenComplete((r, e) -> System.out.println("whenCompleted thread:" +

Is there a way that we can convert from futures 0.1 to the standard library futures?

人走茶凉 提交于 2020-01-25 08:57:45
问题 The async / await feature is coming soon, but there are a lot of libraries still using futures 0.1. How do we convert between the two? Convert an async future to 0.1 future covers converting an async future to 0.1 future. How do I erase the type of future in the new future API? talks about an async function that calls an 0.1 future and gets the result, but where is the await!() macro I can import? It seems it no longer compiles. struct A_future01; impl A_future01 { pub fn exec1() -> Box<dyn

Future solutions

纵然是瞬间 提交于 2020-01-25 05:33:06
问题 I am working with a large data set, which I use to make certain calculations. Since it is a huge data set, my machine, I am working on, is doing the job excessively long, for this reason I decided to use the future package in order to distribute the work between several machines and speed up the calculations. So, my problem is that through the future (using putty & ssh) I can connect to those machines (in parallel), but the work itself is doing the main one, without any distribution. Maybe

How do I store a variable of type `impl Trait` in a struct?

笑着哭i 提交于 2020-01-25 03:09:26
问题 This works: let fut = Arc::new(Mutex::new(Box::pin(async { 1 }))); let mut conn_futures = BTreeMap::new(); // implicitly typed conn_futures.insert(123, fut); if let Some(fut) = conn_futures.get_mut(&123) { let fut = fut.clone(); self.pool.spawn(async move { let mut fut = fut.try_lock().unwrap(); (&mut *fut).await; }); }; How do I write the same thing inside a structure; what is the type of conn_futures ? According to the compiler, it's BTreeMap<i32, impl Future> , but there's no way to write

How can I create a stream where the items are based on items that the stream previously returned?

☆樱花仙子☆ 提交于 2020-01-24 11:48:05
问题 I have a function that generates a futures::Stream based on an argument. I want to call this function multiple times and flatten the streams together. Complicating matters is the fact that I want to feed the values returned by the stream back as the argument to the original function. Concretely, I have a function that returns a stream of numbers down to zero: fn numbers_down_to_zero(v: i32) -> impl Stream<Item = i32> { stream::iter((0..v).rev()) } I want to call this function starting at 5.

How to implement a stream of futures for a blocking call using futures.rs and Redis PubSub?

落爺英雄遲暮 提交于 2020-01-22 14:39:30
问题 I'm trying to create a system by which my application can receive streaming data from a Redis PubSub channel and process it. The Redis driver that I'm using, along with all other Redis drivers for Rust that I've seen, use a blocking operation to get data from the channel that only returns a value when it receives data: let msg = match pubsub.get_message() { Ok(m) => m, Err(_) => panic!("Could not get message from pubsub!") }; let payload: String = match msg.get_payload() { Ok(s) => s, Err(_)

ExecutorService.submit(Task) vs CompletableFuture.supplyAsync(Task, Executor)

依然范特西╮ 提交于 2020-01-22 05:36:29
问题 To run some stuff in parallel or asynchronously I can use either an ExecutorService: <T> Future<T> submit(Runnable task, T result); or the CompletableFuture Api: static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor); (Lets assume I use in both cases the same Executor) Besides the return type Future vs. CompletableFuture are there any remarkable differences. Or When to use what? And what are the differences if I use the CompletableFuture API with default Executor