Java thread pool to wait for termination

1, the polling isTerminated:

        while (!service.isTerminated()) {
            TimeUnit.SECONDS.sleep(1);
        }

2, the polling awaitTermination:

        while (!service.awaitTermination(1, TimeUnit.SECONDS)) {
        }

It represents a service thread pool object when the thread pool out of the while loop will automatically terminate. The principle of these two methods is actually the same. I am here to set up a poll once every 1s, you can control the polling interval as small as possible, in order to promptly respond to the logic thread pool after termination. Since both methods are blocked, so do not forget to interrupt exception handling.

Intelligent Recommendation

Java thread creation and termination

Java thread creation and termination 3 ways to create threads 1. Inherit the Thread class 2. Implement the Runnable interface 3. Use Callable and Future 3 ways to terminate threads 1. Use the stop met...

Termination Java thread

The thread will be ended normally after the thread is executed, and it will be destroyed after the thread is over, but the Run () method will never end, or the time required to cycle, when the thread ...

Termination of thread in Java

Termination of thread The method of thread pauses in Java is the Sleep method, and the SLEEP method is a static method of the Thread class. The following is in the form of code, using the SLEEP method...

Java termination thread method

Java termination thread method 1. STOP () method for Thread instance object This method allows thread to throw out the ireths of Error ThreadDeath abnormally to violent termination thread, threaddeath...

More Recommendation

Java-wait for wake-up case, thread pool, Lambda expression

[Thread pool, Lambda expression] Chapter One Waiting for Wake Up Mechanism 1.1 Inter-thread communication concept: Multiple threads are processing the same resource, but the processing actions (thread...

About the use of thread pool execution and termination methods

———— There are usually two execution methods of thread pool: execute and submit, so what is the difference between them? ———— Is there any way to stop t...

Termination of thread pool execution - actual combat

Termination thread pool execution First, the closure and termination of the difference between thread pool 1. Thread pool close 2. Thread pool termination Second, the termination thread pool execution...

Lock pool and wait pool in Java

In the Java platform, each object can assume the function of a lock because of the built-in lock mechanism. The Java virtual machine maintains two "queues" for each object (let's call it a &...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top