7 Replies Latest reply: Jan 7, 2008 4:08 AM by 807603 RSS

    Thread Pool Is Not Terminating

    807603
      Hi All;

      I am trying to stop java program which are running continuously in a while loop and loop is looking for a stop file.
      When I am trying to stop java program through a Unix stop script, which will create a stop file .
      It is working fine with normal program where threading is not involved but multi threaded program(Via Thread pool) it is not working.

      Could you please refer below sample program which using Thread pool,

      ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5, 5, 10,
                               TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
                     threadPool.prestartAllCoreThreads();
                     File f=new File("/app/cbf/stop");
                     while(!f.isFile()){
                     new JavaTest().start(threadPool);
                     }


      Could any body please suggest me on this.

      Thanx & Regards
        • 1. Re: Thread Pool Is Not Terminating
          807603
          What you should do is to create a background thread or Timer, which checks for the file, say, every 30 seconds. When it finds that shutdown is requested it needs to call shutDown() on the ThreadPoolExecutor as well as whatever else is needed to shut down.

          Obviously at some stage your initialisation must start at least one actual process.
          • 2. Re: Thread Pool Is Not Terminating
            807603
            Sorry for incomplete information, in my code i have one stop method which calling shutDown method to stop thread pool but in our case programm is not coming out from loop after placing the stop file in above mentioned directory .....


                      Properties p = new Properties();
                      p.keys();
                      try {
                           ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5, 5, 10,
                                     TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
                           threadPool.prestartAllCoreThreads();
                           File f=new File("C:/stop");
                           while(!f.isFile()){
                           new JavaTest().start(threadPool);
                           }
                           new JavaTest().stop(threadPool);
            • 3. Re: Thread Pool Is Not Terminating
              807603
              This code doesn't make sense to me. You only need to start the executor once, not every time you detect the file is not there yet.

              And you don't want to have a loop consisting only of a test for file instance only, that's going to waste resources like crazy. Check only at some sensible interval (e.g. every 15 seconds).

              What's more I can't see that your submitting any jobs for the executor to run
              • 4. Re: Thread Pool Is Not Terminating
                807603
                Here is the complete code,

                import java.io.File;
                import java.io.IOException;
                import java.util.Properties;
                import java.util.concurrent.LinkedBlockingQueue;
                import java.util.concurrent.ThreadPoolExecutor;
                import java.util.concurrent.TimeUnit;

                public class JavaTest {

                     public void start(ThreadPoolExecutor threadPool) {

                          threadPool.execute(new JavaThread());
                     }

                     public void stop(ThreadPoolExecutor threadPool) {
                          threadPool.shutdown();
                          System.out.println("Stopping thread pool");
                     }

                     /**
                     * @param args
                     */
                     public static void main(String[] args) throws IOException {
                          // TODO Auto-generated method stub

                          Properties p = new Properties();
                          p.keys();
                          try {
                               ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5, 5, 10,
                                         TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
                               threadPool.prestartAllCoreThreads();
                               File f = new File("C:/stop");
                               while (!f.isFile()) {
                                    new JavaTest().start(threadPool);
                               }
                               new JavaTest().stop(threadPool);
                          } catch (Exception ex) {
                               ex.printStackTrace();
                          }

                     }
                }
                • 5. Re: Thread Pool Is Not Terminating
                  807603
                  Please consider above code as sample program ,in our application we are using all resources of pool in proper manner. but when ever we are trying to stop our programm through shell script by putting stop file in a dedicated location ,it is not stopping the java process and in this case we have to stop process by using kill -9 PID, which is not a good practice.


                  Pls advice
                  • 6. Re: Thread Pool Is Not Terminating
                    807603
                    while (!f.isFile()) {
                    new JavaTest().start(threadPool);
                    }
                    new JavaTest().stop(threadPool);
                    This doesn't make sense to me. It's should look more like;
                     
                    JavaTest mainClass = new JavaTest();
                    mainClass.start(threadPool);
                    while(!f.isFile()) {
                         Thread.sleep(15000);
                         }
                    mainClass.stop(threadPool);
                    Edited by: malcolmmc on Jan 7, 2008 10:22 AM
                    • 7. Re: Thread Pool Is Not Terminating
                      807603
                      Thanx for you now its working fine.