Home  >  Article  >  Java  >  What is thread pool in java

What is thread pool in java

(*-*)浩
(*-*)浩Original
2019-11-12 09:03:012266browse

Thread pool is a form of multi-threading processing that adds tasks to the queue during processing and then automatically starts these tasks after the thread is created. Thread pool threads are background threads. Each thread uses the default stack size, runs at the default priority, and is in a multi-threaded apartment.

What is thread pool in javaIf a thread is idle in managed code (such as waiting for an event), the thread pool will insert Another worker thread to keep all processors busy.

If all thread pool threads remain busy at all times, but the queue contains pending work, the thread pool will create another worker thread after a while but the number of threads will never exceed the maximum. Threads that exceed the maximum can be queued, but they are not started until other threads have finished. (Recommended learning:

java course

)

The use of four thread pools in Java:


Java provides four thread pools through Executors , respectively:


newCachedThreadPool creates a cacheable thread pool. If the thread pool length exceeds processing needs, idle threads can be flexibly recycled. If there is no recycling, a new thread will be created.


newFixedThreadPool creates a fixed-length thread pool that can control the maximum number of concurrent threads. Exceeding threads will wait in the queue.


newScheduledThreadPool creates a fixed-length thread pool that supports scheduled and periodic task execution.


newSingleThreadExecutor creates a single-threaded thread pool, which will only use the only working thread to execute tasks, ensuring that all tasks are executed in the specified order (FIFO, LIFO, priority).


newCachedThreadPool

Create a cacheable thread pool. If the length of the thread pool exceeds processing needs, idle threads can be flexibly recycled. If there is no recycling, create a new one. thread. The sample code is as follows:

package test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolExecutorTest {
    public static void main(String[] args) {
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            final int index = i;
            try {
                Thread.sleep(index * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            cachedThreadPool.execute(new Runnable() {
                public void run() {
                    System.out.println(index);
                }
            });
        }
    }
}

The above is the detailed content of What is thread pool in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn