?
Solved

Issue using two instances of Executor service one after the other?

Posted on 2011-10-12
3
Medium Priority
?
304 Views
Last Modified: 2012-05-12
In my code, I have two instances of of Executor Service with cached thread pools.

For example,

I have my service calling the runner object which implements runnable. I call runProcess method twice in a row. My question is if this is if one service will interfere with the other service. I need the threads in each service to finish one after the other. Please let me know.

void runProcess(){

try{
ExecutorService service = Executors.newCachedThreadPool();
TestRunner runner = new Runner(object obj);
//some code
}
finally{

service.shutdown();
service.awaittermination(1,TIMEUNIT.HOURS)

}
}
0
Comment
Question by:pkrish80
3 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 36954029
They shouldn't interfere with each other, but if your intention is simply to create two threads, why not just use ONE ExecutorService?
0
 

Author Comment

by:pkrish80
ID: 36956075
I need to create two services with multiple threads. Both the set of the threads will be processing different datasets. Will they run sequentially per code above?
0
 
LVL 10

Accepted Solution

by:
gordon_vt02 earned 400 total points
ID: 36962082
There are two ways you can ensure the threads execute sequentially.  The first is to use a fixed-size thread pool with a single thread.  The easiest way to accomplish that is

Executors.newSingleThreadExecutor();

Open in new window


That Executor will run submitted jobs one at a time in the order they were received.

The second option is to use a Latch or other blocking mechanism to prevent the second thread from running until it is notified that the first thread has completed.  This approach adds a lot of usually unnecessary complexity and I'd recommend the SingleThreadExecutor if you truly need the tasks to run sequentially.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question