executorservice

Wait for all the threads to finish their job and then measure the time elapsed

只谈情不闲聊 提交于 2019-12-13 00:25:21
问题 I am using Executor service in one of my Multithreaded code . I am trying to see whether all of my threads are finished doing their job then afterwards I need to do certain task. Currently I need to measure the time elapsed, so I can only measure the time elapsed only after all the threads has finished executing there job. So I have this code currently? And I am measuring time elapsed in the finally block . ExecutorService service = Executors.newFixedThreadPool(noOfThreads); long startTime =

Android application is hanged and showing black screen for 5-6 seconds when checking if a server port is open or not using ThreadPoolExecutor

我的未来我决定 提交于 2019-12-13 00:12:43
问题 I need to run a service if a server port is open. I am doing this by using below method. public Future<Boolean> ifPortIsOpenThenStartIridiumService(final Context context, final String device_mac, final String device_imei, final String input_mobile) { return Executors.newFixedThreadPool(20).submit(new Callable<Boolean>() { @Override public Boolean call() { try { String SERVER_IP = "IP Address"; int SERVER_PORT = Server_port; int DURATION = 1000; Socket socket = new Socket(); socket.connect(new

Java Executor and Long-lived Threads

怎甘沉沦 提交于 2019-12-12 20:00:30
问题 I've inherited some code that uses Executors.newFixedThreadPool(4); to run the 4 long-lived threads that do all the work of the application. Is this recommended? I've read the Java Concurrency in Practice book and there does not seem to be much guidance around how to manage long-lived application threads. What is the recommended way to start and manage several threads that each live for the entire live of the application? 回答1: You mentioned that code is using Executors , it should be

How to execute code after response in JavaEE

不问归期 提交于 2019-12-12 16:28:48
问题 I tried, unsuccessfully, to find a way to run code after response. In my case, a server sends me data in order to let me do my job but this action can be long (send SMS to 5000 contacts and check who received it, for example). The server expects HTTP 204 No Content response immediately to be sure that data has been received. Then my webapp will performs the action and send the status on a REST API. My problem is: How to send the response and then execute code ? For now I tried: doFilter

Detect if current thread is ExecutorService thread

我是研究僧i 提交于 2019-12-12 15:47:15
问题 I want to ensure all methods of my class (updater service) are called within the ExecutorService 's thread (provided there's even a single thread). The order of the methods is not given so those that are public might get called from both the Executor 's thread and some other threads, mostly GUI thread. My code: // Instantiated lazy using synchronized getter - I decided not to bother with shared locks private ExecutorService executor; /** * Provides compatibility between calls from executor

How to make Concurrency utilities (JSR 236) work in plain non-EE Tomcat 8?

守給你的承諾、 提交于 2019-12-12 12:13:25
问题 My technical stack includes Tomcat 8.5.47 (plain, not EE) Weld 2.4.8.Final (as a reference implementation of CDI 1.2) To be able to use ManagedExecutorService and manage threads more wisely and in a safer manner, I would like to include this dependency org.glassfis:javax.enterprise.concurrent (as a reference implementation of JSR 236 [Concurrency Utilities]) I tried to inject the bean in different ways and into differently-scoped beans (view-, session-, application-based). @Inject private

ThreadPool does not run tasks in sequence

☆樱花仙子☆ 提交于 2019-12-12 11:31:17
问题 I am using the Executor framework specifically Executors.newCachedThreadPool(); I have a list of Runnable s e.g. 100. The first 50, each create a value (stored in a list) to be used by the last 50. I thought that if I pass the Runnable s in the executor.execute() in the order they are in the list, they would be also executed in the same order. But this is not happening. The tasks seem to be executed in random order and they are interleaved, not executed in sequence. Is this how it is suppose

Producer-Consumer using Executor Service

元气小坏坏 提交于 2019-12-12 04:29:17
问题 I am learning Executor service and trying to create producer-consumer scenario using Executor Service. I have defined producer and consumer with run method that keeps running till flag "isStopped" is not set false. My Producer class has run method as follows @Override public void run() { while(!isStopped){ MyTask task = new MyTask(); System.out.println("I am thread "+this+" entering task "+ task); try { myBlockingQueue.addTask(task); } catch (InterruptedException e) { // TODO Auto-generated

Proxy object throws NullPointerException

落花浮王杯 提交于 2019-12-12 03:36:01
问题 I'm using Java 1.8. Here is my interface which extends ExecutorService : public interface ConsumerExecutor extends ExecutorService{ boolean closed(); } Here is my InvocationHandler class: public class ExecutorInvocationHandler implements InvocationHandler { private boolean close = false; private ExecutorService executor; public ExecutorInvocationHandler(ExecutorService exec) { this.executor = exec; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

Handling exceptions from Java ExecutorService tasks

拥有回忆 提交于 2019-12-12 02:39:08
问题 I'm trying to use Java's ThreadPoolExecutor class to run a large number of heavy weight tasks with a fixed number of threads. Each of the tasks has many places during which it may fail due to exceptions. I've subclassed ThreadPoolExecutor and I've overridden the afterExecute method which is supposed to provide any uncaught exceptions encountered while running a task. However, I can't seem to make it work. For example: public class ThreadPoolErrors extends ThreadPoolExecutor { public