7 Replies Latest reply: Jan 12, 2010 4:28 AM by r035198x RSS

    Know if work in a ThreadPoolExecutor is finished

    807580
      Hi,

      I have a thread pool which is constructed in the following way:
      ThreadPoolExecutor myThreadPool = new ThreadPoolExecutor(POOL_SIZE, POOL_SIZE, 5, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(WORK_QUEUE_SIZE));
      I'm using the execute method in order to run tasks using the thread pool. I want to know how can I know if all work is done. I cannot wait for each task to finish, instead I have to get this information from the thread pool.

      I tried the following code:
      (myThreadPool.getQueue().size() > 0) && (myThreadPool.getActiveCount() > 0)
      but I'm not sure if it is good enough.

      Thanks.
        • 1. Re: Know if work in a ThreadPoolExecutor is finished
          r035198x
          What do you want to use the information for?

          If you know how many tasks are going to be submitted you can use getCompletedTaskCount
          which should eventually reach your submitted tasks count if all tasks are completed.
          • 2. Re: Know if work in a ThreadPoolExecutor is finished
            807580
            I don't know the number of tasks
            • 3. Re: Know if work in a ThreadPoolExecutor is finished
              r035198x
              Again what do you want to use the information for?
              If the tasks are being submitted by some random process then knowing that all currently submitted tasks are completed
              might not be very useful since a task could then get submitted soon after making your check.
              I think you have to take the source of your tasks into consideration depending on what you want to use the information for.
              • 4. Re: Know if work in a ThreadPoolExecutor is finished
                807580
                r035198x wrote:
                Again what do you want to use the information for?
                If the tasks are being submitted by some random process then knowing that all currently submitted tasks are completed
                might not be very useful since a task could then get submitted soon after making your check.
                I think you have to take the source of your tasks into consideration depending on what you want to use the information for.
                I have to disagree. The approach I use is that shown in the Javadoc i.e.
                        pool.shutdown(); // Stops any further tasks being added
                            pool.awaitTermination(100, TimeUnit.MINUTES); // Waits 100 minutes - change the value and units to suit.
                • 5. Re: Know if work in a ThreadPoolExecutor is finished
                  r035198x
                  sabre150 wrote:
                  ..
                          pool.shutdown(); // Stops any further tasks being added
                  pool.awaitTermination(100, TimeUnit.MINUTES); // Waits 100 minutes - change the value and units to suit.
                  Isn't that cheating?

                  If the OP's requirements allow shutting it down first then sure.
                  The problem is that the OP has not stated those requirements well enough, IMO.

                  Edited by: r035198x on Jan 12, 2010 12:14 PM has stated ---------> has not stated
                  • 6. Re: Know if work in a ThreadPoolExecutor is finished
                    807580
                    r035198x wrote:
                    sabre150 wrote:
                    ..
                            pool.shutdown(); // Stops any further tasks being added
                    pool.awaitTermination(100, TimeUnit.MINUTES); // Waits 100 minutes - change the value and units to suit.
                    Isn't that cheating?
                    No! It's what the Javadoc says to do.

                    >
                    If the OP's requirements allow shutting it down first then sure.
                    True - but if it's not then he can never know if all the tasks have finished since without the shutdown() more can be added at any time.
                    The problem is that the OP has stated those requirements well enough, IMO.
                    To me there is an implied requirement that he is not going to add more tasks. In these forums if we waited for people to detail their requirements then we would never be able answer anything. It is quite common that a poster does not understand the implications of his question so one has to try to lead him in a direction that will make him understand. If this shutdown() is not a requirement then the OP will quite quickly find out during testing and then at that point I expect the OP to explain why he can't use shutdown().

                    I gave the hint of the methods to use and mentioned the Javadoc so I expect the OP to go an read the Javaoc. If he does not then that is his problem.
                    • 7. Re: Know if work in a ThreadPoolExecutor is finished
                      r035198x
                      sabre150 wrote:
                      .In these forums if we waited for people to detail their requirements then we would never be able answer anything...
                      ++
                      And given that he took eight days to respond to the first reply ...