-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
--- | ||
title: ThreadPoolExecutor线程池 | ||
article: true | ||
icon: editor | ||
date: 2023-09-24 | ||
category: | ||
- JAVA学习 | ||
|
||
tag: | ||
- 多线程 | ||
- JUC | ||
--- | ||
|
||
# ThreadPoolExecutor线程池 | ||
|
||
> 参考:[什么是线程池](https://javabetter.cn/thread/pool.html#%E4%B8%80%E3%80%81%E4%BB%80%E4%B9%88%E6%98%AF%E7%BA%BF%E7%A8%8B%E6%B1%A0) | ||
## 1. 什么是线程池 | ||
|
||
线程池其实是一种池化的技术实现,**池化技术的核心思想就是实现资源的复用,避免资源的重复创建和销毁带来的性能开销**。线程池可以管理一堆线程,让线程执行完任务之后不进行销毁,而是继续去处理其它线程已经提交的任务。 | ||
|
||
## 2. 线程池运行原理 | ||
|
||
|
||
## 3. ThreadPoolExecutor执行方法 | ||
|
||
```java | ||
// 一共分为三步 | ||
public void execute(Runnable command) { | ||
// 首先检查提交的任务是否为null,是的话则抛出NullPointerException。 | ||
if (command == null) | ||
throw new NullPointerException(); | ||
// 获取线程池的状态,包括线程池状态(?),工作线程数量,初始数量为负数 | ||
// get获取线程的value | ||
int c = ctl.get(); | ||
|
||
// 1. 检查当前运行的工作线程数是否少于核心线程数(corePoolSize) | ||
if (workerCountOf(c) < corePoolSize) { | ||
// 少于核心线程数量则添加worker | ||
if (addWorker(command, true)) | ||
return; | ||
c = ctl.get(); | ||
} | ||
|
||
// 2. 尝试将任务添加到任务队列中 | ||
if (isRunning(c) && workQueue.offer(command)) { | ||
int recheck = ctl.get(); | ||
// 双重检查线程池的状态 | ||
if (! isRunning(recheck) && remove(command)) // 如果线程池已经停止,从队列中移除任务, && 左面为true才会执行remove函数移除任务 | ||
reject(command); | ||
// 如果线程池正在运行,但是工作线程数为0,尝试添加一个新的工作线程 | ||
else if (workerCountOf(recheck) == 0) | ||
addWorker(null, false); | ||
} | ||
// 3. 如果任务队列满了,尝试添加一个新的非核心工作线程来执行任务 | ||
else if (!addWorker(command, false)) | ||
reject(command); | ||
} | ||
``` | ||
|
||
```java | ||
private static final int CAPACITY = (1 << COUNT_BITS) - 1; | ||
private static int workerCountOf(int c) { return c & CAPACITY; } //与运算获取线程数 | ||
``` | ||
|
||
```java | ||
private static final int SHUTDOWN = 0 << COUNT_BITS; | ||
private static boolean isRunning(int c) { return c < SHUTDOWN; } //小于0,即为线程池正在运行 | ||
``` | ||
|
||
```java | ||
workQueue.offer(command) // 能够添加返回True,不能添加返回False | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
--- | ||
icon: edit | ||
date: 2023-09-24 | ||
article: true | ||
category: | ||
- 项目分享 | ||
tag: | ||
- SpringBoot2 | ||
- Swagger3 | ||
--- | ||
|
||
# Swagger接口文档分享 | ||
|
||
## 1. Swagger 整合 | ||
swagger官网: https://swagger.io/ | ||
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。 | ||
### 1.1 maven 依赖导入 | ||
```xml | ||
<dependency> | ||
<groupId>io.springfox</groupId> | ||
<artifactId>springfox-boot-starter</artifactId> | ||
<version>3.0.0</version> | ||
</dependency> | ||
``` | ||
|
||
### 1.2 application 配置 | ||
```yml | ||
springfox: | ||
documentation: | ||
swagger-ui: | ||
enabled: true | ||
``` | ||
### 1.2 Swagger Config 配置 | ||
```java | ||
import io.swagger.annotations.ApiOperation; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.HttpMethod; | ||
import springfox.documentation.builders.*; | ||
import springfox.documentation.oas.annotations.EnableOpenApi; | ||
import springfox.documentation.schema.ScalarType; | ||
import springfox.documentation.service.*; | ||
import springfox.documentation.spi.DocumentationType; | ||
import springfox.documentation.spring.web.plugins.Docket; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* @author: SunHB | ||
* @createTime: 2023/09/24 上午11:00 | ||
* @description: | ||
*/ | ||
@Configuration | ||
@EnableOpenApi | ||
public class Swagger3Config { | ||
|
||
|
||
@Bean | ||
public Docket createRestApi() { | ||
//返回文档摘要信息 | ||
return new Docket(DocumentationType.OAS_30) | ||
.apiInfo(apiInfo()) | ||
.select() | ||
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) | ||
//.apis(RequestHandlerSelectors.basePackage("com.ytkj.controller")) | ||
.paths(PathSelectors.any()) | ||
.build() | ||
.globalRequestParameters(getGlobalRequestParameters()) | ||
.globalResponses(HttpMethod.GET, getGlobalResponseMessage()) | ||
.globalResponses(HttpMethod.POST, getGlobalResponseMessage()); | ||
} | ||
|
||
/** | ||
* 生成接口信息,包括标题、联系人等 | ||
*/ | ||
private ApiInfo apiInfo() { | ||
return new ApiInfoBuilder() | ||
.title("yantu测试接口文档") | ||
.description("如有疑问,可联系孙鸿博") | ||
.version("1.0") | ||
.build(); | ||
} | ||
|
||
/** | ||
* 封装全局通用参数 | ||
*/ | ||
private List<RequestParameter> getGlobalRequestParameters() { | ||
List<RequestParameter> parameters = new ArrayList<>(); | ||
return parameters; | ||
} | ||
|
||
/** | ||
* 封装通用响应信息 | ||
*/ | ||
private List<Response> getGlobalResponseMessage() { | ||
List<Response> responseList = new ArrayList<>(); | ||
responseList.add(new ResponseBuilder().code("404").description("未找到资源").build()); | ||
return responseList; | ||
} | ||
|
||
|
||
} | ||
``` | ||
|
||
|
||
## 2. Swagger 展示接口方法 | ||
|
||
@Api:用在类上,说明该类的作用。 | ||
|
||
@ApiOperation:注解来给API增加方法说明。 | ||
|
||
@ApiImplicitParams : 用在方法上包含一组参数说明。 | ||
|
||
@ApiImplicitParam:用来注解来给方法入参增加说明。 | ||
|
||
@ApiResponses:用于表示一组响应 | ||
|
||
```java | ||
@RestController | ||
@Slf4j | ||
@RequestMapping("/test") | ||
@Api(tags = "测试接口管理") | ||
public class TestController | ||
``` | ||
|
||
```java | ||
@ApiOperation(value = "主控测试接口") | ||
@PostMapping("/chat") | ||
@CrossOrigin | ||
public Result<Map> testChat | ||
``` | ||
|
||
```java | ||
@ApiOperation(value = "文档模块测试接口") | ||
@PostMapping("v1/docQA") | ||
@CrossOrigin | ||
public Result docQA | ||
``` | ||
|
||
访问: http://localhost:port/swagger-ui/index.html | ||
![ES 框架图](/assets/project/swagger.png) |