Home  >  Article  >  Java  >  What is inheritance of classes in Java thread pool?

What is inheritance of classes in Java thread pool?

PHPz
PHPzforward
2023-05-09 10:55:07651browse

1. Description

The core implementation class of the thread pool in Java is ThreadPoolExecutor

Executor: only provides an interface for executing tasks, and users do not need to pay attention to how to create threads. How to make a thread, just provide a Runnable object.

ExecutorService: On the basis of task execution, interfaces such as task submission and thread pool life cycle management are added.

AbstractExecutorService: The process of abstract series execution tasks, ensuring that the implementation of the lower layer only needs to focus on the method of executing tasks.

ThreadPoolexecutor: On the one hand, it maintains its own life cycle, and on the other hand, it manages courses and tasks. The two are well combined to implement parallel tasks.

2. Example

// ctl:高三位表示线程池运行状态,低29位表示线程池线程运行数量
// 一个变量存储两个值的好处是不必费心思(比如加锁)去维护两个状态的一致性
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
 
// 获取线程池当前的运行状态(~:按位取反,即0变成1,1变成0。)
private static int runStateOf(int c)     { return c & ~CAPACITY; }
// 获取线程池当前运行的线程数量
private static int workerCountOf(int c)  { return c & CAPACITY; }
// 通过线程池状态和运行的线程数量获取ctl
private static int ctlOf(int rs, int wc) { return rs | wc; }

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

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete