(Java multi-threaded series nine) thread pool

Thread Pool

1. What is the thread pool

It refers to reuse the thread pool threads to create a set of threads in a multithreaded application initialization process, then you need to perform new tasks, rather than create a new thread. The number of threads in a thread pool usually depends on the amount of memory available and the needs of the application. Each thread in the pool have been assigned a task, once the task has been completed, the thread pool in the back and once assigned tasks waiting.

2, the role of the thread pool

① thread pool to improve the response time of an application. As the threads in the pool is ready and waiting to be assigned tasks, the application can be directly used without using a new thread.

Each short life cycle task to create a complete thread ② thread pool, and resources can be recovered after the task is completed.

③ thread pool thread time slice is optimized according to processes that are currently running on the system.

④ thread pool allows us to open multiple tasks without having to set the properties for each thread.

3, Java provides four thread pool

3.1、newCachedThreadPool

Creating a cache thread pool, thread pool longer than if treatment needs, the flexibility to reclaim idle thread, if not recyclable, the new thread.

public class CachedThreadPoolDemo {

    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newCachedThreadPool();
        for (int i = 1; i <= 10; i++) {
            final int index = i;
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + "=====" + index);
                }
            });
        }
    }

}
3.2、newFixedThreadPool

Create a fixed-length thread pool, you can control the maximum number of concurrent threads, excess threads will wait in the queue.

public class FixedThreadPoolDemo {
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        for (int i = 1; i <= 10; i++) {
            final int index = i;
            threadPool.execute(() -> {
                System.out.println(Thread.currentThread().getName() + "=====" + index);
            });
        }
    }
}

3.3、newScheduledThreadPool

Create a fixed-length thread pool to support regular and periodic task execution.

public class ScheduledThreadPoolDemo {

    public static void main(String[] args) {
        ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(3);
        for (int i = 1; i <= 10; i++) {
            threadPool.schedule(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + ":延迟3秒打印log");
                }
            }, 3, TimeUnit.SECONDS);
        }
    }

}
3.4 newSingleThreadExecutor

Creating a single-threaded thread pool, it will only work with a single thread to perform the task to ensure that all tasks are performed in a specified order.

public class SingleThreadExecutorDemo {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        for (int i = 1; i <= 10; i++) {
            final int index = i;
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + "===" + index);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}

Source Address

Guess you like

Origin www.cnblogs.com/3LittleStones/p/12105520.html