threadpoolexecutor

How to implement blocking ThreadPoolExecutor

99封情书 提交于 2019-12-11 06:55:46
问题 I need to implement blocking ThreadPoolExecutor . This is a very crucial requirement in our enterprise application. It would do something like if ThreadPoolExecutor.submit() or ThreadPoolExecutor.execute() method blocks until a thread gets freed up for picking up a new task. But in current implementation ThreadPoolExecutor.submit() and ThreadPoolExecutor.execute() methods throw RejectedExecutionException exception if all pooled threads get busy. For example following code throws

Java: Wait in a loop until tasks of ThreadPoolExecutor are done before continuing

喜你入骨 提交于 2019-12-11 06:38:58
问题 I'm working on making the Dijkstra algorithm parallel. Per node threads are made to look at all the edges of the current node. This was made parallel with threads but there is too much overhead. This resulted in a longer time than the sequential version of the algorithm. ThreadPool was added to solve this problem but i'm having trouble with waiting until the tasks are done before I can move on to the next iteration. Only after all tasks for one node is done we should move on. We need the

Python concurrent.futures.ThreadPoolExecutor max_workers

橙三吉。 提交于 2019-12-11 06:03:40
问题 I am searching for a long time on net. But no use. Please help or try to give me some ideas how to achieve this. When I use python module concurrent.futures.ThreadPoolExecutor(max_workers=None) , I want to know the max_workers how much the number of suitable. I've read the official document. I still don't know the number of suitable when I coding. Changed in version 3.5: If max_worker is None or not give, it will default to the number of processors on the machine, multiplied by 5, assuming

ThreadPoolExecutor application does not Finish

怎甘沉沦 提交于 2019-12-11 03:52:48
问题 This super simple application prints "Hello" but does not finish. I see absolutely no reason why this should be. JavaDoc, section finalization, says that A pool that is no longer referenced in a program AND has no remaining threads will be shutdown automatically. tpe is clearly not referenced, that means that thread does not finish. But I don't understand why. Could someone explain? Solution in this case is to call shutdown() at the end of main, but my actual application is more complicated.

Why ThreadPoolExecutor behaves differently when running Java program in Eclipse and from command line?

青春壹個敷衍的年華 提交于 2019-12-11 03:46:27
问题 Can anybody explain why this code throws OOME when I run it from Eclipse (Juno) but works fine when I run it from command line? I use -Xmx256M in both cases. static class Task implements Runnable { byte[] buf = new byte[150000000]; @Override public void run() { } } public static void main(String[] args) throws Exception { System.out.println(Runtime.getRuntime().maxMemory()); ExecutorService ex = Executors.newSingleThreadExecutor(); ex.submit(new Task()).get(); ex.submit(new Task()).get(); }

Download bunch of files from server

僤鯓⒐⒋嵵緔 提交于 2019-12-11 02:27:22
问题 I am trying to download a bunch of files from a PHP server and store them on an sdcard. But when I download the files, I am getting the following error : No of files download from server is **500 -600** after download some files **250 - 260** . It raised an error. Error 07-10 08:40:46.228: E/File Error(1837): File id is 287 File path is http://192.168.1.21/SOCH/upload/chapter_36/5.9.pdf Synch Path is /mnt/sdcard/SOCH/36/ 07-10 08:40:46.268: W/System.err(1837): java.util.concurrent

ThreadPoolExecutor with context manager

本小妞迷上赌 提交于 2019-12-10 20:26:16
问题 I don't understand why this code is behaving in different way. In the first case, the code will print 'elo' and after 19 seconds we will see '3'. In other case we will be first wait 19 seconds, and after that we will see 'elo'. Could you explain me that? from concurrent.futures import ThreadPoolExecutor def wait_on_future(): f = 3 import time time.sleep(19) print(f) executor = ThreadPoolExecutor(max_workers=2) executor.submit(wait_on_future) print("elo") vs from concurrent.futures import

newFixedThreadPool.setCorePoolSize() doesn't make use of the threads, creates new theads which may be overhead

人走茶凉 提交于 2019-12-10 14:42:30
问题 newFixedThreadPool.setCorePoolSize() doesn't make use of the threads, creates new theads. Explanation: I make a newFixedThreadPool for size 2 and if both the threads of this pool are busy I add two more threads to this pool using setCorePoolSize(). In this process it doesn't seem to reuse the threads or may be terminating some threads and creating new which I will explain with code. Code: (Please also see the Output for understanding) public class IncreasePoolSize { static ExecutorService

RejectedExecutionException 128 Active Threads AsyncTaskLoader

橙三吉。 提交于 2019-12-10 11:14:17
问题 I've searched around for solutions to RejectedExecutionException using AsyncTaskLoader but none have worked. Most of them are for AsyncTask like https://github.com/commonsguy/cwac-task/blob/master/src/com/commonsware/cwac/task/AsyncTaskEx.java but AsyncTaskLoader is kind of different. The first thing I tried was in onCreateLoader() doing this before returning a CursorLoader. if (loaderCount >= 100) { cursorLoader.setUpdateThrottle(1000000000); } All that really does is stop the loaders after

ThreadPoolExecutor的PriorityBlockingQueue支持问题

醉酒当歌 提交于 2019-12-10 08:46:02
最近在使用ThreadPoolExecutor时遇到一个问题:当ThreadPoolExecutor使用的BlockingQueue为PriorityBlockingQueue时,会出现异常,原因是java.util.concurrent.FutureTask cannot be cast to java.lang.Comparable。Google之,发现有很多同样的问题,但没有给出解决方案,只能查看源代码以期能找到并解决问题。 首先根据Exception找到问题原因: Exception in thread "main" java.lang.ClassCastException: java.util.concurrent.FutureTask cannot be cast to java.lang.Comparable at java.util.concurrent.PriorityBlockingQueue.siftUpComparable(PriorityBlockingQueue.java:347) at java.util.concurrent.PriorityBlockingQueue.offer(PriorityBlockingQueue.java:475) at java.util.concurrent.ThreadPoolExecutor.execute