简体   繁体   中英

Thread pool issue in java

HI the following code is used in a application

threadPool.shutdown();
        while (!threadPool.awaitTermination(10, TimeUnit.SECONDS)) {
            logger.info("Waiting for "
                    + (threadPool.getQueue().size() + threadPool
                            .getActiveCount()) + " jobs to complete.");
        }

when application is running it strucked in the loop

  • Waiting for 134 jobs to complete.
  • Waiting for 134 jobs to complete.
  • Waiting for 134 jobs to complete.

the above statement is coming continuosly,actually thread is performing update operation on database.will increasing the time to more than 10sec will help in this situation.any suggestion is helpful

The documentation for ThreadPoolExecutor.shutdown() says:

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down.

Which means that your worker threads are responsible for shutting themselves down. You haven't provided the code for your workers, but I'm assuming they are performing a blocking operation that doesn't exit. A common approach to solving this is to have the workers routinely check in on a shared variable of some kind that is set just before the shutdown call. They are effectively told to stop whatever they're working on. An AtomicBoolean should do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM