0

I am using the ExecutorService. I am opening 10 threads for a for-loop. So it opens 10 threads and when does the connection closes here? At executor.shutdown()? If yes, will it closes all 10 threads and open 10 threads again? Is there anything I can follow better coding standards in order to increase the performance here?

ExecutorService executor = Executors.newFixedThreadPool(10);
for(String str : stringList){
     for(String str1 : stringList1){
         for(String str2 : stringList2){
              executor.execute(new Runnable() {
                        @Override
                        public void run() {
                        //do something
                        }
          }
     }
}
executor.shutdown();
executor.awaitTermination(24L, TimeUnit.HOURS);

Actually my idea is to iterate through the loop and save the data in a file and save it for every iteration. So I want to follow the best practices here as I don't want to see any server related issues like connection issues/outOfMemory exceptions in later stages. Please suggest.

I have implemented this by myself but I am a noob in multi-threading. So to get some clarification after seeing some other blogs and posts I have posted this. Please help!

0

2 Answers 2

1

Executors.newFixedThreadPool(10) does exactly what it sounds like - it creates a thread pool, with a maximum of 10 threads. From the documentation:

The threads in the pool will exist until it is explicitly shutdown

So, best practice is to share the thread pool where possible. Don't do this:

for (int i = 0; i < 8; i++) {
    ExecutorService executor = Executors.newFixedThreadPool(10);

    for (String str1 : stringList1) {
        for (String str1 : stringList1) {
            executor.execute(new Runnable(){
                public void run(){ /* Some work... */ }
            });
        }
    }
    executor.shutdown();
    executor.awaitTermination(24L, TimeUnit.HOURS);
}

Notice that a new FixedThreadPool is created and destroyed 8 times within the for-loop, and creating/destroying the thread pools is creating/destroying up to 10 threads. This is bad, because it defeats the point of using a thread pool. If you're going to use a pool within a loop, make sure you re-use it where you can:

ExecutorService executor = Executors.newFixedThreadPool(10);

for (int i = 0; i < 8; i++) {
    for (String str1 : stringList1) {
        for (String str1 : stringList1) {
            executor.execute(new Runnable(){
                public void run(){ /* Some work... */ }
            });
        }
    }
}

executor.shutdown();
executor.awaitTermination(24L, TimeUnit.HOURS);

And finally, one last thing to watch out for is the amount of work you queue. If you are queueing work faster than the thread pool can process it, then the queue size will slowly grow, until you reach an OutOfMemoryException. Unfortunately, there's no built-in way to check the number of tasks remaining in the thread pool, so you'll have to work that one out on your own.

This concept applies to the server as well - if the number of incoming requests per second is higher than the number of requests the server can process per second, for a long enough duration, then the server will run out of resources and crash. The solution is usually to have automatic scaling and load balancing, and knowing how to configure these things is a very useful skill to have in the industry.

1

when does the connection closes here? At executor.shutdown()? If yes, will it closes all 10 threads and open 10 threads again?

Once you call ExecutorService.html#shutdown, no new new tasks are submitted to the pool and once no tasks in the pool, ExecutorService will be shutdown.

void shutdown()

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down.

This method does not wait for previously submitted tasks to complete execution. Use awaitTermination to do that.

Once shutdown is triggered, it won't open 10 threads again unless you re-create ExecutorService

Actually my idea is to iterate through the loop and save the data in a file and save it for every iteration. So I want to follow the best practices here as I don't want to see any server related issues like connection issues/outOfMemory exceptions in later stages

Don't do it. Let's wait for ExecutorService to properly shutdown.

Other way to address your issue : Wait for completion of your tasks. You can find more details in:

How to properly shutdown java ExecutorService

On a different note:

newFixedThreadPool creates unbounded ExecutorService. Look out for other alternatives in:

How to properly use Java Executor?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.