Java P
Java P

Reputation: 2281

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.

Upvotes: 5

Views: 11566

Answers (2)

Ben Aiad
Ben Aiad

Reputation: 78

ExecutorServise will have a pool of Threads to execute your Runnable tasks, you defined the size of the pool to be 4, I would change that to be the number of cpus in the machine:

int threadCount = Runtime.getRuntime().availableProcessors();

ExecutorService executor = Executors.newFixedThreadPool(threadCount);

Also, it appears that you are sending Threads to the executor to execute them in one of the available threads in it's pool, It's redundant, you might change your MyThread to a Runnable task.

Upvotes: 1

Manjunath
Manjunath

Reputation: 1685

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

Upvotes: 4

Related Questions