Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExecutorService fixed pool threads are hanging

I am using following code snippet in our code.

ExecutorService executor = Executors.newFixedThreadPool(4);
while(loop 50 times){
    //In the extreme case I will have 50 threads and only 4 will be active and remaining are in queue 
    MyThread myThread = new MyThread();
    executor.execute(myThread);//Each Thread process 100,000 records and put in a file
}
executor.shutdown();
while (!executor.isTerminated()) {
}

Here are my questions:

  1. It is hanging at second while loop. What might be the reason?
  2. Is there any way to terminate Entire thread pool after certain interval?
  3. Can I terminate a thread after certain time interval?

Please help me in fixing this.

like image 605
Java P Avatar asked Aug 22 '14 16:08

Java P


People also ask

How do I stop the ExecutorService thread?

To properly shut down an ExecutorService, we have the shutdown() and shutdownNow() APIs. This method returns a list of tasks that are waiting to be processed. It is up to the developer to decide what to do with these tasks.

Is it necessary to shutdown ExecutorService?

Upon termination, an executor has no tasks actively executing, no tasks awaiting execution, and no new tasks can be submitted. An unused ExecutorService should be shut down to allow reclamation of its resources. Method submit extends base method Executor. execute(java.

What are the advantages of using ExecutorService instead of creating threads directly?

Below are some benefits: Executor service manage thread in asynchronous way. Use Future callable to get the return result after thread completion. Manage allocation of work to free thread and resale completed work from thread for assigning new work automatically.


1 Answers

1.It is hanging at second while loop. What might be the reason?

The reason for hanging could be because of very few threads compared to the amount of records that needs to be processed and stored in file. If each thread is supposed to process 100,000 records and put in file then 50 thread tasks shared by 4 threads will have to process 5,000,000 records with 50 files. So better to increase the number of threads and check. Also note down time taken by each thread to effectively measure if you are reducing the time taken overall by increasing the number of fixed pool threads.

2.Is there any way to terminate Entire thread pool after certain interval?

Yes below code shows that:-

executor.shutdown();
executor.awaitTermination(60, TimeUnit.SECONDS); // blocks/waits for certain interval as specified
executor.shutdownNow(); // Forcefully terminate entire thread pool after the above time.

3.Can I terminate a thread after certain time interval?

Yes if effectively the reason for terminating a thread is to stop what task it is doing. To achieve this we need to get a reference to the future and wait conditionally for period of time before we forcefully cancel the task and interrupt the thread carrying out the task.

        Map<String, Future> tasks = new HashMap<String, Future>();
        while(i++ < 50){
            //In the extreme case I will have 50 threads and only 4 will be active and remaining are in queue 
            Thread myThread = new Thread();
            tasks.put("Thread"+i ,executor.submit(myThread));//Each Thread process 100,000 records and put in a file
        }
        // say you want to terminate Thread2 after 60 seconds
        Future thread2Task = tasks.get("Thread2");
        thread2Task.get(60, TimeUnit.SECONDS);
        thread2Task.cancel(true); // boolean whether you want to interrupt the thred forcefully
like image 200
Manjunath Avatar answered Oct 06 '22 00:10

Manjunath