executorservice

Spring Async issue when upgrading from 4.2.0.RC3 to 4.2.0.RELEASE

霸气de小男生 提交于 2019-11-29 00:26:52
问题 I've a web application using the spring(4.2.x) artifacts spring-webmvc, spring-messaging, spring-websocket I've the below @Enable* annotations in my spring config java class @EnableWebMvc @EnableWebSocketMessageBroker @EnableAsync @EnableMBeanExport WebSocket is used for broadcasting messages to browser clients. And there are few async methods annotated with @Async The application was working fine with spring version 4.2.0.RC3. But when I changed it to the GA release 4.2.0.RELEASE, I get the

Stop an infinite loop in an ExecutorService task

荒凉一梦 提交于 2019-11-29 00:13:01
import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; class Task implements Callable<String> { public String call() throws Exception { String s = "initial"; try { System.out.println("Started.."); /*for (int i=0;i<10000;i++) { if (i % 2 == 0) { System.out.println("Even"); } }*/ boolean flag = true; while(flag) { } System.out.println("Finished!"); s = "Done"; } catch

Java - splitting work to multiple threads

百般思念 提交于 2019-11-28 21:35:45
I am posed with the following problem: I need to split work across multiple threads for perfomance reasons, but I am not sure what approach to take. Firstly, the task I would be supplying should return a value and take a parameter. Additionally, the main method (doing the main bit of work, not static main() ) is already running on separate thread and is invoked periodically. Also, this method must at some point WAIT for all threads to finish and then proceed. One approach (most obvious to me) is to schedule each job on a separate thread and store results in class vars: public Object result1,

Java executors: wait for task termination. [duplicate]

。_饼干妹妹 提交于 2019-11-28 18:53:04
This question already has an answer here: How to wait for all threads to finish, using ExecutorService? 24 answers I need to submit a number of task and then wait for them until all results are available. Each of them adds a String to a Vector (that is synchronized by default). Then I need to start a new task for each result in the Vector but I need to do this only when all the previous tasks have stopped doing their job. I want to use Java Executor, in particular I tried using Executors.newFixedThreadPool(100) in order to use a fixed number of thread (I have a variable number of task that can

How to pause/resume all threads in an ExecutorService in Java?

喜你入骨 提交于 2019-11-28 18:46:57
I submitted bunch of jobs to an executorservice in Java and I somehow want to temporarily pause all these jobs. What's the best way to do this? How can I resume? Or am I doing this completely wrong? Should I follow some other pattern for what I want to achieve (i.e. ability to pause/resume execution services)? To answer my own question, I found an example of a PausableThreadPoolExecutor in the javadocs of ThreadPoolExecutor itself . Here is my version using Guava's Monitors: import com.google.common.util.concurrent.Monitor; import java.util.concurrent.ScheduledThreadPoolExecutor; import java

What's the difference between Future and FutureTask in Java?

你。 提交于 2019-11-28 17:45:11
Since use ExecutorService can submit a Callable task and return a Future , why need to use FutureTask to wrap Callable task and use the method execute ? I feel they both do the same thing. In fact you are correct. The two approaches are identical. You generally don't need to wrap them yourself. If you are, you're likely duplicating the code in AbstractExecutorService: /** * Returns a <tt>RunnableFuture</tt> for the given callable task. * * @param callable the callable task being wrapped * @return a <tt>RunnableFuture</tt> which when run will call the * underlying callable and which, as a <tt

ExecutorService vs ThreadPoolExecutor using LinkedBlockingQueue

不羁岁月 提交于 2019-11-28 17:10:54
I am working on a multithreaded project in which I need to spawn multiple threads to measure the end to end performance of my client code, as I'm doing Load and Performance testing. So I created the below code which is using ExecutorService . Below is the code with ExecutorService : public class MultithreadingExample { public static void main(String[] args) throws InterruptedException { ExecutorService executor = Executors.newFixedThreadPool(20); for (int i = 0; i < 100; i++) { executor.submit(new NewTask()); } executor.shutdown(); executor.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); } }

java code execution yields to different results in debug without breakpoints and normal run. Is ExecutorService broken?

谁说胖子不能爱 提交于 2019-11-28 16:30:46
问题 TL:DR ExecutorService executorService = Executors.newFixedThreadPool(8); in debug runs concurrent, but in normal runtime it starts concurrent, but later runs in single thread. I have some code where I start 4 different tasks in ExecutorService . Two of those tasks should finish almost instantly, the other two should run for a while. Those tasks return execution time in seconds in Future<Double> . This code is responsible for task execution and measurement: public Future<Double> measure(int[]

shutdown and awaitTermination which first call have any difference?

允我心安 提交于 2019-11-28 16:22:43
What is the difference between ExecutorService eService = Executors.newFixedThreadPool(2); eService.execute(new TestThread6()); eService.execute(new TestThread6()); eService.execute(new TestThread6()); eService.awaitTermination(1, TimeUnit.NANOSECONDS); eService.shutdown(); and eService.shutdown(); eService.awaitTermination(1, TimeUnit.NANOSECONDS); I don't really understand shutdown() . This method does not wait for previously submitted tasks to complete execution. Does it mean shutdown() may terminate the tasks which have been submitted, but not completed? I tried some examples, they do not

How to get thread id from a thread pool?

放肆的年华 提交于 2019-11-28 14:22:44
问题 I have a fixed thread pool that I submit tasks to (limited to 5 threads). How can I find out which one of those 5 threads executes my task (something like "thread #3 of 5 is doing this task")? ExecutorService taskExecutor = Executors.newFixedThreadPool(5); //in infinite loop: taskExecutor.execute(new MyTask()); .... private class MyTask implements Runnable { public void run() { logger.debug("Thread # XXX is doing this task");//how to get thread id? } } 回答1: Using Thread.currentThread() :