Home > Backend Development > PHP Tutorial > Questions about thread pools that may be encountered in PHP interviews

Questions about thread pools that may be encountered in PHP interviews

不言
Release: 2023-04-03 09:40:01
Original
3118 people have browsed it

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.

Preface

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.

What is a thread pool

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 collection that executes multiple threads for an application.

Why do we need a thread pool?

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:


Questions about thread pools that may be encountered in PHP interviews

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).

  1. 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.

  2. 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.

  3. The thread pool optimizes thread time slices based on the processes currently running in the system.

  4. Thread pool allows us to start multiple tasks without setting properties for each thread.

  5. The thread pool allows us to pass an object reference containing state information for the program parameters of the task being executed.

  6. 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:

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.

Disadvantages of single-threading

Give me an example

new Thread(new Runnable() {

@Override
public void run() {
   paPaPaYourGridFriend();
}
}).start();
Copy after login
Say important things three times! ! !

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!

The disadvantages are as follows:
  • 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.

Java thread pool

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

创建一个大小无限的线程池。此线程池支持定时以及周期性执行任务的需求。

newSingleThreadExecutor
  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; i <h5>ScheduledExecutorService</h5><p><img src="https://segmentfault.com/img/remote/1460000015702768" alt=" " title=" "><br></p><h5>newFixedThreadPool</h5><p><img src="https://segmentfault.com/img/remote/1460000015702769" alt=" " title=" "></p><h5><img src="https://segmentfault.com/img/remote/1460000015702770" alt=" " title=" "></h5><p   style="max-width:90%"><strong>newCachedThreadPool</strong></p><p><img src="https://segmentfault.com/img/remote/1460000015702771" alt=" " title=" "><br></p><h6>相比new Thread,Java提供的四种线程池的好处在于:</h6>
Copy after login
  • a. 重用存在的线程,减少对象创建、消亡的开销,性能佳。

  • b. 可有效控制最大并发线程数,提高系统资源的使用率,同时避免过多资源竞争,避免堵塞。

  • c. 提供定时执行、定期执行、单线程、并发数控制等功能。

线程池真的是太好用了,如果在项目中通过线程池管理线程,,你将会发现其中的诸多优势!

阅读更多

20+个很棒的Android开源项目

2018年Android面试题含答案--适合中高级(下)一份完整的Android Studio搭建Flutter教程[](http://mp.weixin.qq.com/s?__b...

深入了解JAVA的线程中断方法经验之总结

子线程为什么不能更新UI线程详解

相信自己,没有做不到的,只有想不到的

相关推荐:

php如何连接数据库的方法

PHP中http的数据库是如何进行验证登录

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template