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

Posted on 2011-10-12
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(){

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


Question by:pkrish80
    LVL 86

    Expert Comment

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

    Author Comment

    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?
    LVL 10

    Accepted Solution

    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


    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    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…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now