Status of thread pool (5 kinds):
The AtomicInteger variable ctl is very powerful: the low 29 bits represent the number of threads in the thread pool, and the high 3 bits represent the running state of the thread pool:
- RUNNING: -1 << COUNT_ Bits, that is, the upper 3 bits are 111. The thread pool in this state will receive new tasks and process the tasks in the blocking queue;
- SHUTDOWN: 0 << COUNT_ Bits, that is, the upper 3 bits are 000. The thread pool in this state will not receive new tasks, but will process the tasks in the blocking queue;
- STOP : 1 << COUNT_ Bits, that is, the upper 3 bits are 001. The thread in this state will not receive new tasks, nor process the tasks in the blocking queue, and will interrupt the running tasks;
- TIDYING : 2 << COUNT_ Bits, that is, the upper 3 bits are 010. This status indicates that the thread pool arranges and optimizes threads;
- TERMINATED: 3 << COUNT_ Bits, that is, the upper 3 bits are 011, which indicates that the thread pool stops working;
How to create a thread pool
Executors factory class
- Executors.newCachedThreadPool(); Note: create a cacheable thread pool. If the length of the thread pool exceeds the processing needs, you can flexibly recycle idle threads. If there is no recyclable thread, you can create a new thread
- Executors.newFixedThreadPool(int); Note: create a fixed length thread pool to control the maximum concurrent number of threads. Excess threads will wait in the queue.
- Executors.newSingleThreadExecutor(); Note: create a singleton thread pool. It will only use a unique working thread to execute tasks to ensure that all tasks are executed in order.
- Executors.newScheduledThreadPool(int); Description: create a fixed length routing pool to support regular and periodic task execution.
- FixedThreadPool and SingleThreadPool:
The allowed request queue length is Integer.MAX_VALUE, which may accumulate a large number of requests, resulting in OOM. 2. CachedThreadPool and ScheduledThreadPool: the number of threads allowed to create is Integer.MAX_VALUE, a large number of threads may be created, resulting in OOM.
Although the above Executors provide tool classes, there are still problems that may lead to OOM. We generally recommend using the thread pool inside the SDK. At the same time, users can understand the meaning of each parameter to prevent problems. The SDK provides the construction method of creating thread pool, as follows:
/** * @param corePoolSize: Number of core threads; * @param maximumPoolSize: The maximum number of threads allowed in the thread pool; * @param keepAliveTime: Keep active time, idle threads (normal threads) are more than keepAliveTime If it is not reused in time, it will be destroyed; * @param unit: Time unit; * @param workQueue: Task queue is used to store tasks that have been submitted and will be executed; * @param threadFactory: Thread factory, which is used to create threads in the thread pool; * @param handler: Thrown when the thread pool is closed, or the maximum number of threads and queue are saturated RejectedExecutionException Abnormal; > **Java Network disk: pan.baidu.com/s/1MtPP4d9Xy3qb7zrF4N8Qpg > Extraction code: 2 p8n** ### last **[CodeChina Open source project: [first tier big factory] Java Analysis of interview questions+Core summary learning notes+Latest explanation Video]]( )** ### ActiveMQ message middleware interview topic * What is? ActiveMQ? * ActiveMQ What if the server goes down? * What about losing the message? * What if persistent messages are very slow? * What about the uneven consumption of news? * What about the dead letter queue? * ActiveMQ Is the message retransmission interval and number of retransmissions in? **ActiveMQ Analysis and expansion of message oriented middleware interview topics:** ![BAT Interview documents: ActiveMQ+redis+Spring+Highly concurrent multithreading+JVM](https://img-blog.csdnimg.cn/img_convert/8efa9a2953ecb956b70cefd577a9129c.png) * * * # redis interview topics and answers * What are the clients that support consistent hashing? * Redis With others key-value How is storage different? * Redis How is your memory usage? * What are the ways to reduce Redis What about your memory usage? * see Redis What commands are used for usage and status information? * Redis What happens when you run out of memory? * Redis Is single threaded, how to improve multi-core CPU Utilization of? ![BAT Interview documents: ActiveMQ+redis+Spring+Highly concurrent multithreading+JVM](https://img-blog.csdnimg.cn/img_convert/b45eb8f2023a61c83f1e1c3143f1ba5a.png) * * * # **Spring interview topics and answers** * Talk about your right Spring Understanding of * Spring What are the advantages? * Spring Design patterns in * How to open annotation assembly and common annotations * A brief introduction Spring bean Life cycle of **Spring Analysis and expansion of interview answers** ![BAT Interview documents: ActiveMQ+redis+Spring+Highly concurrent multithreading+JVM](https://img-blog.csdnimg.cn/img_convert/cb59884f4c4079976be33cc3bb2bcd67.png) * * * # Highly concurrent multithreaded interview topics * Now there are threads T1,T2 and T3. How do you ensure T2 Thread in T1 After that, and T3 Thread in T2 After that? * Java Zhongxin Lock Interface relative to synchronous code block( synchronized block)What are the advantages? If you implement a high-performance cache that supports concurrent reads and single writes, how can you ensure data integrity. * Java in wait and sleep What's the difference? * How in Java Implement a blocking queue in? * How in Java Write code to solve producer consumer problems? * Write a piece of deadlock code. What are you doing Java How to resolve deadlock in? **Analysis and expansion of highly concurrent multithreaded interview** ![BAT Interview documents: ActiveMQ+redis+Spring+Highly concurrent multithreading+JVM](https://img-blog.csdnimg.cn/img_convert/be8acb44f45db0641d658317959d642c.png) * * * # jvm interview topics and analysis * JVM What are the components? * JVM Memory partition? * Java Memory model for? * Classification of references? * GC when does it start? **JVM Analysis and expansion of interview topics!** ![BAT Interview documents: ActiveMQ+redis+Spring+Highly concurrent multithreading+JVM](https://img-blog.csdnimg.cn/img_convert/3f8cee2db0c7431645e37921121b2ea9.png) ？ * JVM Memory partition? * Java Memory model for? * Classification of references? * GC when does it start? **JVM Analysis and expansion of interview topics!** [External chain picture transfer...(img-8CrTQx4E-1631407408752)]