futuretask

Java FutureTask completion check

China☆狼群 提交于 2019-12-23 12:16:54
问题 I have checked Oracle Java API, which gives some info that FutureTask.isDone() but I need to check whether the task has completed or terminated with any error. isDone() method will return even if it completes/terminates. But I need to know whether it's completed or terminated due to some problem. 回答1: If the FutureTask is done, call get() afterwards (can be without any timeout, it should return immediately). It will either return some result or throw: ExecutionException - if the computation

cancel() not working while trying to cancel tasks in ThreadPoolExecutor Android

时光毁灭记忆、已成空白 提交于 2019-12-13 01:24:21
问题 I have a few downloads that are submitted as tasks to a ThreadPoolExecutor . Now, I am creating this ThreadPoolExecutor in a global class that extends Application . I am storing all the submitted tasks in a hashmap with ids. I have a list view in a fragment. This list view item contains pause and resume buttons. When I click on the list item itself, the download FutureTask is submitted to the global pool executor. Now, when I click on the pause button of the listview item, I want that

Computation with Futures avoiding Await method

☆樱花仙子☆ 提交于 2019-12-12 06:56:59
问题 I have funtion which is calling computeParallel() function which is calling 3 Futures F1,F2,F3 and returning String as return type. def computeParallel():String = { val f1 = Future { "ss" } val f2 = Future { "sss" } val f3 = Future { "ssss" } val result: Future[String] = for { r1 <- f1 r2 <- f2 r3 <- f3 } yield (r1 + r2 + r3) Await.result(result,scala.concurrent.duration.Duration.Inf) } Using Await to collect the Aggregated Results.But People are saying usage of Await is Bad way of coding. So

traversing a List<Future> object throws IndexOutOfBounds exception

一个人想着一个人 提交于 2019-12-10 15:41:57
问题 I have an ExecutorService which is used to invoke a Collection of Callable obejcts and returns a List of Future objects corresponding to Callable elements in the collection. However, somewhere while traversing the list, it throws the following exception : java.util.concurrent.ExecutionException: java.lang.IndexOutOfBoundsException: Index: 7, Size: 1 at java.util.concurrent.FutureTask.report(Unknown Source) at java.util.concurrent.FutureTask.get(Unknown Source) at com.soc.transformcsv

how to convert java Future<V> to guava ListenableFuture<V>

最后都变了- 提交于 2019-12-08 16:00:37
问题 I need to find a way to convert from Future to ListenableFuture. Currently i'm using a service which returns Future but i need to hook up a listener to it. I can't change the service interface as it doesn't belong to me. Is there a simple way to do that ? I have read guava docs but still i can't find a way to do it. 回答1: Guava provides the JdkFutureAdapters types for this conversion. The API states Utilities necessary for working with libraries that supply plain Future instances. For example

Underlying thread behavior with Future.get(timeout)

女生的网名这么多〃 提交于 2019-12-08 07:37:37
问题 We are using Future with a timeout to accomplish a task. We get a TimeOutException when time limit exceeds. From the behavior of thread dump , I realize that underlying thread continues. Is it the case? How does it take care of multiple threads roaming around? What if no IOException is thrown for the thread which was removed from the pool? If this is true, what is the way to kill underlying thread. It keeps on waiting for an external IO in my case. A part of thread dump: Thread 29587: (state

Difference between TimerTask and Executors.newScheduledThreadPool(1)

筅森魡賤 提交于 2019-12-03 16:36:36
问题 I need to schedule some work to be done in the future. I can do it in 2 ways: Create a TimerTask and execute timer.schedule(...); Use Executors.newScheduledThreadPool(1) : ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); ScheduledFuture <?> scheduleHandle = scheduler.schedule(pushExternalRunnable, runScheduleDate.getTime() - now.getTime(), TimeUnit.MILLISECONDS); What is the difference between these 2 ways of scheduling the work in the future? 回答1: The biggest

Android BluetoothSocket - Timing out

◇◆丶佛笑我妖孽 提交于 2019-11-30 14:25:27
问题 I have written a Bluetooth API for connecting with an external accessory. The way that the API is designed is that there are a bunch of blocking calls such as getTime , setTime , getVolume , setVolume , etc. The way these work is that they create a payload to send and call a method called sendAndReceive() which does some prep work and eventually does the following: byte[] retVal = null; BluetoothSocket socket = getSocket(); // write socket.getOutputStream().write(payload); // read response if

Android BluetoothSocket - Timing out

馋奶兔 提交于 2019-11-30 10:28:35
I have written a Bluetooth API for connecting with an external accessory. The way that the API is designed is that there are a bunch of blocking calls such as getTime , setTime , getVolume , setVolume , etc. The way these work is that they create a payload to send and call a method called sendAndReceive() which does some prep work and eventually does the following: byte[] retVal = null; BluetoothSocket socket = getSocket(); // write socket.getOutputStream().write(payload); // read response if(responseExpected){ byte[] buffer = new byte[1024]; // buffer store for the stream int readbytes =

java Callable FutureTask Excecuter: How to listen to finished task

爱⌒轻易说出口 提交于 2019-11-30 09:07:33
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 executer some extra Callable to get notified about the task finishing? What's the way to go here? I