threadpoolexecutor

ThreadPoolExecutor with unbounded queue not creating new threads

江枫思渺然 提交于 2019-11-30 07:05:46
问题 My ThreadPoolExecutor is failing to create new threads. In fact I wrote a somewhat hacky LinkedBlockingQueue that will accept any task (i.e. it is unbounded) but call an additional handler - which in my application spews warning trace that the pool is behind - which gives me very explicit information that the TPE is refusing to create new threads even though the queue has thousands of entries in it. My constructor is as follows: private final ExecutorService s3UploadPool = new

What's the advantage of a Java-5 ThreadPoolExecutor over a Java-7 ForkJoinPool?

倖福魔咒の 提交于 2019-11-29 20:09:53
Java 5 has introduced support for asynchronous task execution by a thread pool in the form of the Executor framework, whose heart is the thread pool implemented by java.util.concurrent.ThreadPoolExecutor. Java 7 has added an alternative thread pool in the form of java.util.concurrent.ForkJoinPool. Looking at their respective API, ForkJoinPool provides a superset of ThreadPoolExecutor's functionality in standard scenarios (though strictly speaking ThreadPoolExecutor offers more opportunities for tuning than ForkJoinPool). Adding to this the observation that fork/join tasks seem to be faster

How to stop next thread from running in a ScheduledThreadPoolExecutor

狂风中的少年 提交于 2019-11-29 17:36:21
I have a ScheduledThreadPoolExecutor which has one thread and runs for every 30 seconds. Now, if the current executing thread throws some exception, then I need to make sure that the next thread do not run and the the ScheduledThreadPoolExecutor is down. How do I achieve this? As a clean way, you can simply use a static accessed class to set/check the execution availability. import java.util.concurrent.atomic.AtomicBoolean; class ThreadManager { private static AtomicBoolean shouldStop = new AtomicBoolean(false); public static void setExceptionThrown(boolean val) { shouldStop.set(val); } public

SingleThreadExecutor VS plain thread

半城伤御伤魂 提交于 2019-11-29 16:58:10
问题 Apart from the fact that the Executor interface has some advantages over plain threads (management, for example), is there any real internal difference (big performance difference, resource consumption...) between doing: ExecutorService executor = Executors.newSingleThreadExecutor(); executor.submit(runnable); And: Thread thread = new Thread(runnable); thread.start(); I'm only asking about a single thread here. 回答1: Executors#newSingleThreadExecutor() creates ThreadPoolExecutor object under

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

陌路散爱 提交于 2019-11-29 12:29:24
问题 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); }

How to can I run adb screenrecord on a Android Device from Java and end the screenrecording?

孤人 提交于 2019-11-29 12:20:11
How to can I run adb shell screenrecord on a Android Device from Java and end the screenrecording ? Currently I have to specify the --time-limit to end the recording. If I try to capture the video prior to it ending it fails. Is there a way to tell adb to stop recording? Is there a way to configure the thread to send a CTRL-C to the shell command? This is to be run on unrooted phones. Code videoExecutor.submit(() -> { //Start recording video String recordVideo = "screenrecord --time-limit " + testRun.getVideoLength() + " " + "/sdcard/" + logDate + ".mp4"; try { device.executeShell(recordVideo)

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

假如想象 提交于 2019-11-29 11:45: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

Testing PriorityBlockingQueue in ThreadPoolExecutor

我是研究僧i 提交于 2019-11-29 04:54:24
I realized my ThreadPoolExecutor with PriorityBlockingQueue like in this example: https://stackoverflow.com/a/12722648/2206775 and wrote a test: PriorityExecutor executorService = (PriorityExecutor) PriorityExecutor.newFixedThreadPool(16); executorService.submit(new Runnable() { @Override public void run() { try { Thread.sleep(1000); Thread.sleep(1000); System.out.println("1"); } catch (InterruptedException e) { e.printStackTrace(); } } }, 1); executorService.submit(new Runnable() { @Override public void run() { try { Thread.sleep(1000); Thread.sleep(1000); System.out.println("3"); } catch

How many AsyncTasks i can run in an single process application

こ雲淡風輕ζ 提交于 2019-11-29 03:55:41
I am using asyncTasks, to load list elements with images (Just followed android's tutorial of efficiently loading bitmaps) In DDMS, i can see upto 5 AsyncTasks being running Now in addition i have added another AsyncTask, which performs some decoding using MediaCodec class. Now in DDMS, i still see 5 AsyncTasks, and my image loading aynctask or decoding async task executes, not both of them. When decoding is running, if i scroll the list, elements' images are not updated Counterly when i launch new decoding asynctask by calling it's execute method, the decoding doesn't start, but if i scroll

Pre-initializing a pool of worker threads to reuse connection objects (sockets)

僤鯓⒐⒋嵵緔 提交于 2019-11-28 18:53:43
I need to build a pool of workers in Java where each worker has its own connected socket; when the worker thread runs, it uses the socket but keeps it open to reuse later. We decided on this approach because the overhead associated with creating, connecting, and destroying sockets on an ad-hoc basis required too much overhead, so we need a method by which a pool of workers are pre-initializaed with their socket connection, ready to take on work while keeping the socket resources safe from other threads (sockets are not thread safe), so we need something along these lines... public class