Java線程池如何實(shí)現(xiàn)精準(zhǔn)控制每秒API請求
Java中基于線程池實(shí)現(xiàn)指定每秒發(fā)送一定數(shù)量的API請求,可以使用ScheduledExecutorService來調(diào)度任務(wù),同時(shí)使用ThreadPoolExecutor來處理并發(fā)請求,可以根據(jù)實(shí)際需求調(diào)整每秒請求數(shù)量、執(zhí)行時(shí)間、以及線程池大小。
實(shí)現(xiàn)思路
1.創(chuàng)建線程池
- 使用
Executors.newScheduledThreadPool()來創(chuàng)建一個(gè)調(diào)度線程池 - 并使用
Executors.newFixedThreadPool()來創(chuàng)建一個(gè)用于發(fā)送API請求的線程池
2.調(diào)度任務(wù)
- 使用
ScheduledExecutorService來按固定速率調(diào)度任務(wù)。 - 通過控制任務(wù)的頻率,可以確保每秒發(fā)送指定數(shù)量的請求。
3.定義API請求任務(wù)
- 定義一個(gè)實(shí)現(xiàn)
Runnable接口的類 - 負(fù)責(zé)執(zhí)行具體的API請求
4.控制請求速率
- 使用調(diào)度器每秒提交指定數(shù)量的任務(wù)到線程池中執(zhí)行。
引入依賴
<!-- Apache HttpClient -->
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.2</version>
</dependency>實(shí)現(xiàn)代碼
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import java.io.IOException;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
public class ApiRequestScheduler {
// 定義線程池,用于并發(fā)發(fā)送API請求
private final ExecutorService requestExecutor;
// 定義調(diào)度線程池,用于定時(shí)調(diào)度請求任務(wù)
private final ScheduledExecutorService scheduler;
// 記錄已發(fā)送的請求數(shù)量
private final AtomicInteger requestCounter;
// 每秒發(fā)送的請求數(shù)量
private final int requestsPerSecond;
// Apache HttpClient 實(shí)例
private final CloseableHttpClient httpClient;
// API 請求的目標(biāo)URL
private final String apiUrl;
// 構(gòu)造函數(shù),初始化線程池和調(diào)度器
public ApiRequestScheduler(int requestsPerSecond, String apiUrl) {
this.requestsPerSecond = requestsPerSecond;
this.requestExecutor = Executors.newFixedThreadPool(requestsPerSecond);
this.scheduler = Executors.newScheduledThreadPool(1);
this.requestCounter = new AtomicInteger(0);
this.httpClient = HttpClients.createDefault();
this.apiUrl = apiUrl;
}
// 開始調(diào)度API請求任務(wù)
public void start() {
// 每秒調(diào)度任務(wù),按照每秒發(fā)送的請求數(shù)量來執(zhí)行
scheduler.scheduleAtFixedRate(() -> {
for (int i = 0; i < requestsPerSecond; i++) {
requestExecutor.submit(this::sendApiRequest);
}
}, 0, 1, TimeUnit.SECONDS);
}
// 停止調(diào)度和關(guān)閉線程池及HttpClient
public void stop() {
scheduler.shutdown();
requestExecutor.shutdown();
try {
if (!scheduler.awaitTermination(1, TimeUnit.SECONDS)) {
scheduler.shutdownNow();
}
if (!requestExecutor.awaitTermination(1, TimeUnit.SECONDS)) {
requestExecutor.shutdownNow();
}
httpClient.close();
} catch (InterruptedException | IOException e) {
scheduler.shutdownNow();
requestExecutor.shutdownNow();
try {
httpClient.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
// 使用Apache HttpClient發(fā)送API請求
private void sendApiRequest() {
int requestId = requestCounter.incrementAndGet();
HttpUriRequestBase request = new HttpGet(apiUrl);
System.out.println("Sending API request #" + requestId);
try (CloseableHttpResponse response = httpClient.execute(request)) {
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println("Request #" + requestId + " completed with status: " + response.getCode() +
", response: " + responseBody);
} catch (IOException | ParseException e) {
System.err.println("Request #" + requestId + " failed: " + e.getMessage());
}
}
public static void main(String[] args) {
// 每秒發(fā)送5個(gè)API請求,目標(biāo)URL為http://example.com/api
ApiRequestScheduler scheduler = new ApiRequestScheduler(5, "http://www.dzy.com/api");
// 啟動(dòng)調(diào)度器
scheduler.start();
// 運(yùn)行10秒后停止調(diào)度器
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// 停止調(diào)度器
scheduler.stop();
}
}實(shí)現(xiàn)效果
每秒發(fā)送指定數(shù)量的API請求,使用Apache HttpClient處理HTTP通信,并確保在多線程環(huán)境中正確管理資源。
可以根據(jù)實(shí)際需求調(diào)整每秒請求數(shù)量、API URL、以及線程池大小。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis-plus批量去重插入ON DUPLICATE key update使用方式
這篇文章主要介紹了Mybatis-plus批量去重插入ON DUPLICATE key update使用方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Java連接FTP服務(wù)器并使用ftp連接池進(jìn)行文件操作指南
使用FTP最主要的功能是對文件進(jìn)行管理,下面這篇文章主要給大家介紹了關(guān)于Java連接FTP服務(wù)器并使用ftp連接池進(jìn)行文件操作的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08
詳談ThreadLocal-單例模式下高并發(fā)線程安全
這篇文章主要介紹了ThreadLocal-單例模式下高并發(fā)線程安全,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
mybatis中一對一關(guān)系association標(biāo)簽的使用
這篇文章主要介紹了mybatis中一對一關(guān)系association標(biāo)簽的使用,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
解決Spring Security 用戶帳號(hào)已被鎖定問題
這篇文章主要介紹了解決Spring Security 用戶帳號(hào)已被鎖定問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Java多線程編程中synchronized關(guān)鍵字的基礎(chǔ)用法講解
Java的synchronized關(guān)鍵字用于修飾線程同步,用以線程資源共享的目的等,下面就帶來簡單的Java多線程編程中synchronized關(guān)鍵字的基礎(chǔ)用法講解2016-06-06
MyBatis學(xué)習(xí)教程(二)—如何使用MyBatis對users表執(zhí)行CRUD操作
這篇文章主要介紹了MyBatis學(xué)習(xí)教程(二)—如何使用MyBatis對users表執(zhí)行CRUD操作的相關(guān)資料,需要的朋友可以參考下2016-05-05
httpclient getPoolEntryBlocking連接池方法源碼解讀
這篇文章主要為大家介紹了httpclient getPoolEntryBlocking連接池方法源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11

