Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my java program I am using cocurrnent execution of threads by using code below
Java
ExecutorService executor = Executors.newFixedThreadPool(5);
for (int i = 0; i < 5; i++) {
    Thread t1 = new AMDataSend(Q_AM_TO_ID, DATA, MAILER_ID, AM_BATCH_ID, FILE_NAME);
    System.out.println(" thread : " + i);
    executor.execute(t1);
}
executor.shutdown();
System.out.println("pre !executor.isTerminated() : "+(!executor.isTerminated()));
while (!executor.isTerminated()) {

    System.out.println("!executor.isTerminated() : "+(!executor.isTerminated()));
    boolean awaitTermination = executor.awaitTermination(1000, TimeUnit.MILLISECONDS);
    if(awaitTermination){
        break;
    }else{
        System.out.println("not terminated");
    }
}
System.out.println("Finished all threads");


but the executor not get shutdown and while loop act as infinite. How can I solve this problem?
Posted
Updated 1-Jul-13 20:08pm
v3
Comments
Shubhashish_Mandal 2-Jul-13 2:53am    
May child task still active. Shutdown() is wait until any active task is exist in the pool.
Once there are no active task it will showdown. So it is seems to me that there are still some active task exist.
But if you want to shut-down the pool immediately then call shutdownNow() ;

1 solution

Hello,

Check your AMDataSend class. Probably it's waiting for something or has an infinite loop inside. You can modify your termination check loop as follows to forecefully terminate the worker threads.
Java
int cntr = 0;

executor.shutdown(); // Disable new tasks from being submitted
while (!executor.isTerminated() || cntr < 3) {
    boolean awaitTermination = executor.awaitTermination(1000, TimeUnit.MILLISECONDS);
    if (awaitTermination) {
        // All worker threads have completed there unit of work.
        break;
    }
    cntr++;
}
// Check if there are still some lingering threads 
try {
    if (!executor.isTerminated()) {
        executor.shutdownNow(); // Force shutdown
        executor.awaitTermination(1000, TimeUnit.MILLISECONDS); // Wait a while for tasks to respond to being cancelled
    }
}
catch (Exception ex) {
    System.out.println(ex.getMessage());
}

Regards,
 
Share this answer
 
Comments
gavkk 2-Jul-13 3:31am    
I want the complete execution of the threads. They are not supposed to terminate without completetion. If I am using executor.shutdownNow(); , will the threads perform the complete execution as i want?or will they terminate at the moment when executor.shutdownNow(); calls ?
Sometimes each thread may take several minutes.
Prasad Khandekar 2-Jul-13 3:44am    
They will terminate immediately. It's a forceful shutdown. If you want all your threads to complete execution then your code is write. The problem must be in AMDataSend class.

Regards,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900