threadpoolexecutor

ThreadPoolExecutor vs ForkJoinPool: stealing subtasks

 ̄綄美尐妖づ 提交于 2019-12-04 03:50:42
From java docs, A ForkJoinPool differs from other kinds of ExecutorService mainly by virtue of employing work-stealing: all threads in the pool attempt to find and execute subtasks created by other active tasks (eventually blocking waiting for work if none exist). This enables efficient processing when most tasks spawn other subtasks (as do most ForkJoinTasks). When setting asyncMode to true in constructors, ForkJoinPools may also be appropriate for use with event-style tasks that are never joined. After going through below ForkJoinPool example , Unlike ThreadPoolExecutor, I have not seen

ThreadPoolExecutor and the queue

做~自己de王妃 提交于 2019-12-03 13:34:03
问题 I thought that using ThreadPoolExecutor we can submit Runnable s to be executed either in the BlockingQueue passed in the constructor or using the execute method. Also my understanding was that if a task is available it will be executed. What I don't understand is the following: public class MyThreadPoolExecutor { private static ThreadPoolExecutor executor; public MyThreadPoolExecutor(int min, int max, int idleTime, BlockingQueue<Runnable> queue){ executor = new ThreadPoolExecutor(min, max,

How to chose an Executor for CompletableFuture::supplyAsync

无人久伴 提交于 2019-12-03 02:09:07
CompletableFuture::supplyAsync(() -> IO bound queries) How do I chose an Executor for CompletableFuture::supplyAsync to avoid polluting the ForkJoinPool.commonPool() . There are many options in Executors ( newCachedThreadPool , newWorkStealingPool , newFixedThreadPool etc) And I read about new ForkJoinPool here How do I chose the right one for my use case ? You should use public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) method. As executor you can use any from the Executors.new.. - it depends on your needs. It's better to use newFixedThreadPool()

ThreadPoolExecutor : Tasks are getting queued up and not submitted

荒凉一梦 提交于 2019-12-02 23:52:23
We have a scenario where tasks submitted to ThreadPoolExecutor are long running. When the thread pool is started we start it with core pool size = 5, max pool size = 20 and queue size of 10. In our application around 10 tasks get submitted. Most of the time these tasks run for few mins/hrs and then complete. However there was a situation when all of the 5 tasks got hanged on I/O. As a result my core pool size reached it max, but my Threadpoolexecutor queue was not full. So the additional 5 tasks never got a chance to run. Please do suggest how we can handle such scenario? Is having a smaller

get callable from ThreadPoolTaskExecutor or cast Runnable to Callable

蹲街弑〆低调 提交于 2019-12-02 07:04:05
I'm using ThreadPoolTaskExecutor for executing my tasks which are implemantations of Callable interface. I just want to check in time if task is still in pool (monitoring). How to do that? I know that I can get queue from ThreadPoolExecutor but how can I cast Runnable to Callable? Basically I have this callable public interface IFormatter extends Callable<Integer>{ Long getOrderId(); } I'm executing it like this ThreadPoolExecutor.submit(new Formatter(order)); And finally I'd like to loop through queue of ExecutorService in some async method and check if thread with orderId is still there.

How to access running threads inside ThreadPoolExecutor?

不打扰是莪最后的温柔 提交于 2019-12-01 18:41:50
I have a queue of running threads and would like to expose some of its data while it is executed, to monitor the process. ThreadPoolExecutor provides access to its queue and I can iterate through these objects to call my overridden toString() method, but these are only threads that are waiting for execution. Is there a way to access threads that are currently running to call my method? Or maybe there's a better approach for this task in general? To clarify a bit more about the purpose, here's some code of general idea: public class GetDataTask implements Runnable { private String pageNumber;

How to access running threads inside ThreadPoolExecutor?

只谈情不闲聊 提交于 2019-12-01 18:01:07
问题 I have a queue of running threads and would like to expose some of its data while it is executed, to monitor the process. ThreadPoolExecutor provides access to its queue and I can iterate through these objects to call my overridden toString() method, but these are only threads that are waiting for execution. Is there a way to access threads that are currently running to call my method? Or maybe there's a better approach for this task in general? To clarify a bit more about the purpose, here's

android app crashes when exceeded Thread pool 9 and queued tasks 128

妖精的绣舞 提交于 2019-12-01 10:45:27
The application conduction api calls in every 10seconds with the gps location from the network provider. And also there are several api calls can be do by the user. application is crashing with law internet or less internet connection(device data access) is there a proper way to prevent app crashing and hold the api request till the internet network available. here im posting my crash reprort stacktrace java.util.concurrent.RejectedExecutionException: Task android.os.AsyncTask$3@4206a5b0 rejected from java.util.concurrent.ThreadPoolExecutor@41e97858[Running, pool size = 9, active threads = 9,

Executors: How to synchronously wait until all tasks have finished if tasks are created recursively?

旧城冷巷雨未停 提交于 2019-11-30 08:56:17
My question is strongly related to this one here . As was posted there, I would like the main thread to wait until the work queue is empty and all tasks have finished. The problem in my situation is, however, that each task may recursively cause new tasks to be submitted for processing. This makes it a little awkward to collect all of those tasks's futures. Our current solution uses a busy-wait loop to await termination: do { //Wait until we are done the processing try { Thread.sleep(200); } catch (InterruptedException e) { throw new RuntimeException(e); } } while (!executor.getQueue().isEmpty

new Thread(task).start() VS ThreadPoolExecutor.submit(task) in Android

冷暖自知 提交于 2019-11-30 08:26:05
In my Android project I had a lot of places where I need to run some code asynchronously (a web request, call to db etc.). This is not long running tasks (maximum a few seconds). Until now I was doing this kind of stuff with creating a new thread, passing it a new runnable with the task. But recently I have read an article about threads and concurrency in Java and understood that creating a new Thread for every single task is not a good decision. So now I have created a ThreadPoolExecutor in my Application class which holds 5 threads. Here is the code: public class App extends Application {