executorservice

Can't stop a task which is started using ExecutorService

浪子不回头ぞ 提交于 2019-12-06 02:43:47
Sorry I have to open a new thread to describe this problem. This morning I asked this question , there're some replies but my problem is still not solved. This time I will attach some runnable code(simplified but with the same problem) for you to reproduce the problem: public class ThreadPoolTest { public static void main(String[] args) throws Exception { final ExecutorService taskExecutor = Executors.newFixedThreadPool(5); Future<Void> futures[] = new Future[5]; for (int i = 0; i < futures.length; ++i) futures[i] = startTask(taskExecutor); for (int i = 0; i < futures.length; ++i) System.out

Execute a for loop in parallel using CompletableFuture in Java and log the execution

僤鯓⒐⒋嵵緔 提交于 2019-12-05 18:34:15
I have a for loop which I am trying to parallelize using CompletableFuture. for (int i = 0; i < 10000; i++) { doSomething(); doSomethingElse(); } What I have till now is: for (int i = 0; i < 10000; i++) { CompletableFuture.runAsync(() -> doSomething()); CompletableFuture.runAsync(() -> doSomethingElse()); } I guess this serves the purpose but there is a requirement to print log just before the start and end of all the processing. If I do this: log("Started doing things"); for (int i = 0; i < 10000; i++) { CompletableFuture.runAsync(() -> doSomething()); CompletableFuture.runAsync(() ->

How can I report progress from a background task?

时间秒杀一切 提交于 2019-12-05 18:00:17
I have a long running task that is executing in the background on an ExecutorService thread pool. What are some best practices in terms of this task returning progress or intermediate results? Are there any libraries that provide this functionality? EDIT: To clarify, I'm talking about reporting progress to other code, not to the user. Normally I would use SwingWorker, but I'm working with a Java/Groovy backend for a Grails app, and I'm unsure how that would behave in a headless server environment since it has EDT ties. Another example is the Jobs framework in Eclipse RCP, but I would need

How to properly shutdown executor services with Spring?

╄→尐↘猪︶ㄣ 提交于 2019-12-05 17:16:07
问题 I have a command line application that uses a Spring-managed bean that's composed of a java ExecutorService created with: ExecutorService service = Executors.newFixedThreadPool(4); Now, I want my service to shutdown when my application shuts down, so I made my bean implement the DisposableBean interface and have a destroy method such as: public void destroy(){ service.shutdown(); } Then I might be tempted to do something like register a shutdown hook on the Spring context. However I found out

Do we need to shutdown ExecutorService fixedThreadPool

不羁的心 提交于 2019-12-05 15:35:32
I have created threadpool using ExecutorService , in my application to call vendor websrvice, using below code. ExecutorService executor = Executors.newFixedThreadPool(getThreadPoolSize()); for (int i = 0; i < list.size(); i++) { Request ecpReq = list.get(i); thRespLst.add(executor.submit(new Task(ecpReq))); } Wanted to know do we need to take care of shutting down threadpool or something, basically I don't want hanging threads in production environment. Fabio Cardoso newFixedThreadPool public static ExecutorService newFixedThreadPool(int nThreads) Creates a thread pool that reuses a fixed

ForkJoinPool scheduling vs ExecutorService

亡梦爱人 提交于 2019-12-05 13:42:32
I'm slightly confused by the internal scheduling mechanism of the ExecutorService and the ForkJoinPool. I understood the ExecutorService scheduling is done this way . A bunch of tasks are queued. Once a thread is available it will handle the first available task and so forth. Meanwhile, a ForkJoinPool is presented as distinct because it uses a work-stealing algorithm. If I understand correctly it means a thread can steal some tasks from another thread. Yet, I don't really understand the difference with the mechanism implemented in the ExecutorService. From my understanding, both mechanisms

Use only a subset of threads in an ExecutorService

纵然是瞬间 提交于 2019-12-05 07:06:07
In a typical JAVA application, one configures a global ExecutorService for managing a global thread pool. Lets say I configure a fixed thread pool of 100 threads: ExecutorService threadPool = Executors.newFixedThreadPool(100); Now lets say that I have a list of 1000 files to upload to a server, and for each upload I create a callable that will handle the upload of this one file. List<Callable> uploadTasks = new ArrayList<Callable>(); // Fill the list with 1000 upload tasks How can I limit the max number of concurrent uploads to, lets say, 5? if I do threadPool.invokeAll(uploadTasks); I dont

How to better use ExecutorService in multithreading environment?

a 夏天 提交于 2019-12-05 01:29:36
I need to make a library in which I will have synchronous and asynchronous methods in it. executeSynchronous() - waits until I have a result, returns the result. executeAsynchronous() - returns a Future immediately which can be processed after other things are done, if needed. Core Logic of my Library The customer will use our library and they will call it by passing DataKey builder object. We will then construct a URL by using that DataKey object and make a HTTP client call to that URL by executing it and after we get the response back as a JSON String, we will send that JSON String back to

should ExecutorService be static and global

爷,独闯天下 提交于 2019-12-05 00:41:20
I want to use the same thread pool throughout my application. To this end, I can make ExecutorService static and global so that I can invoke ThreadUtil.executorService to get ExecutorService when I need it. public class ThreadUtil { public static final ExecutorService executorService = Executors.newCachedThreadPool(); } Is it OK to instance multiple thread pools like this? In addition, my application is a TCP server. If I don't know how big the pool should be, is it ok to simply use newCachedThreadPool ? When an instance with the same properties is to be used anywhere in your program, it is

ThreadPoolExecutor with ArrayBlockingQueue

允我心安 提交于 2019-12-05 00:07:33
问题 I started reading more about ThreadPoolExecutor from Java Doc as I am using it in one of my project. So Can anyone explain me what does this line means actually?- I know what does each parameter stands for, but I wanted to understand it in more general/lay-man way from some of the experts here. ExecutorService service = new ThreadPoolExecutor(10, 10, 1000L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10, true), new ThreadPoolExecutor.CallerRunsPolicy()); Updated:- Problem