This article introduces to you the issues about thread pools that may be encountered during interviews. It has certain reference value. Friends in need can refer to it.
We often encounter questions about multi-threading and thread pools during interviews. How to answer them? Today, we will learn about the thread pool in Java.
The thread pool refers to creating a collection of threads during the initialization of a multi-threaded application, and then reusing these threads when new tasks need to be performed instead of creating a new thread. . The number of threads in a thread pool usually depends entirely on the amount of available memory and the needs of the application. However, it is possible to increase the number of available threads. Each thread in the thread pool is assigned a task. Once the task has been completed, the thread returns to the pool and waits for the next assigned task.
To put it bluntly, it is a thread collectionthat executes multiple threads for an application.
Using the thread pool, I have currently solved the problem:
Use the thread pool to obtain multiple covers in a video, thereby saving money. It took some time and resources
The background server does not support multiple image uploads. Use the thread pool to upload multiple images, thereby reducing the upload time
The rendering is as follows:
Before uploading: 9 pictures will take at least 3 seconds, use After thread pool optimization, 9 pictures take 1 second.
Using threads in multi-threaded applications is necessary for the following reasons:
1. Reduces the number of threads created and destroyed times, each worker thread can be reused to perform multiple tasks.
2. You can adjust the number of working threads in the thread pool according to the system's capacity to prevent the server from being exhausted due to excessive memory consumption (each thread requires About 1MB of memory. The more threads are opened, the more memory is consumed, and eventually it crashes).
Thread pools improve an application's response time. Since the threads in the thread pool are ready and waiting to be assigned tasks, the application can use them directly without creating a new thread.
The thread pool saves the CLR the overhead of creating a complete thread for each short-lived task and can reclaim resources after the task is completed.
The thread pool optimizes thread time slices based on the processes currently running in the system.
Thread pool allows us to start multiple tasks without setting properties for each thread.
The thread pool allows us to pass an object reference containing state information for the program parameters of the task being executed.
The thread pool can be used to solve the problem of limiting the maximum number of threads to handle a specific request.
Appease the many-year-old Fafafa
The role of the thread pool is to limit execution in the system The number of threads.
According to the system environment, the number of threads can be set automatically or manually to achieve the best operation effect; less will waste system resources, and more will cause system congestion and inefficiency. Use the thread pool to control the number of threads, and other threads wait in line. After a task is executed, the frontmost task in the queue is taken and execution begins. If there is no waiting process in the queue, this resource of the thread pool is waiting. When a new task needs to be run, if there are waiting worker threads in the thread pool, it can start running; otherwise, it enters the waiting queue.
Give me an example
new Thread(new Runnable() { @Override public void run() { paPaPaYourGridFriend(); } }).start();
If you are still using new Thread to perform an asynchronous task, so you are Out!
If you are still using new Thread to perform an asynchronous task, so you are Out!
If you are still using new Thread to perform an asynchronous task, so you are Out!
a. Each time new Thread creates an object, the performance is poor.
b. Threads lack unified management, and there may be unlimited new threads, competing with each other, and may occupy too many system resources, causing crashes or OOM.
c. Lack of more functions, such as scheduled execution, periodic execution, and thread interruption.
1. newSingleThreadExecutor
Create a single-threaded thread pool. This thread pool has only one thread working, which is equivalent to a single thread executing all tasks serially. If the only thread ends abnormally, a new thread will replace it. This thread pool ensures that all tasks are executed in the order in which they are submitted.
2.newFixedThreadPool
创建固定大小的线程池。每次提交一个任务就创建一个线程,直到线程达到线程池的最大大小。线程池的大小一旦达到最大值就会保持不变,如果某个线程因为执行异常而结束,那么线程池会补充一个新线程。
3. newCachedThreadPool
创建一个可缓存的线程池。如果线程池的大小超过了处理任务所需要的线程,
那么就会回收部分空闲(60秒不执行任务)的线程,当任务数增加时,此线程池又可以智能的添加新线程来处理任务。此线程池不会对线程池大小做限制,线程池大小完全依赖于操作系统(或者说JVM)能够创建的最大线程大小。
4.newScheduledThreadPool
创建一个大小无限的线程池。此线程池支持定时以及周期性执行任务的需求。
private void TextnewSingleThreadExecutor(){ ExecutorService pool = Executors. newSingleThreadExecutor(); MyTask1 task1 = new MyTask1(); MyTask2 task2 = new MyTask2(); MyTask3 task3 = new MyTask3(); // pool.execute(task1); // pool.execute(task2); // pool.execute(task3); new Thread(task1).start(); new Thread(task2).start(); new Thread(task3).start(); } private class MyTask1 implements Runnable{ @Override public void run() { //循环输出 for(int i = 0; iScheduledExecutorService
newFixedThreadPool
newCachedThreadPool
相比new Thread,Java提供的四种线程池的好处在于:
a. 重用存在的线程,减少对象创建、消亡的开销,性能佳。
b. 可有效控制最大并发线程数,提高系统资源的使用率,同时避免过多资源竞争,避免堵塞。
c. 提供定时执行、定期执行、单线程、并发数控制等功能。
线程池真的是太好用了,如果在项目中通过线程池管理线程,,你将会发现其中的诸多优势!
20+个很棒的Android开源项目
2018年Android面试题含答案--适合中高级(下)一份完整的Android Studio搭建Flutter教程[](http://mp.weixin.qq.com/s?__b...
深入了解JAVA的线程中断方法经验之总结
子线程为什么不能更新UI线程详解
相关推荐:
The above is the detailed content of Questions about thread pools that may be encountered in PHP interviews. For more information, please follow other related articles on the PHP Chinese website!