gpt4 book ai didi

java - 根据时间限制运行线程

转载 作者:行者123 更新时间:2023-12-03 13:12:34 27 4
gpt4 key购买 nike

我想每秒最多启动40个http请求,并在1秒后,从它自己的队列(例如threadpooltaskexecutor的阻塞队列)中运行另外40个http请求。我正在为此要求寻找执行程序或线程池实现。

有什么建议吗?

谢谢

里阿里

编辑:由于明显的原因,修复率效率不高。当队列项目一个接一个地开始时,队列后面的项目将刚刚开始,但是已经启动了一段时间的项目可能会完成。

额外的编辑:问题是在一秒钟内只调用40个请求,没有激活最大40个。可能在另一秒为80,但在1秒钟内应该只有40个新创建的连接。

最佳答案

一种实现方法是使用另一种体系结构,这将使过程变得更加轻松。

1)创建一个实现可运行对象的Thread类。
2)它以您要发出的HTTP请求列表<>()作为参数
3)使run()函数循环整个列表(大小为40)
4)让线程存活一秒钟。

这是一个示例示例:

class MyClass extends Thread

private ArrayList<...> theList;

public MyClass(ArrayList<..> theList){
this.theList = theList;
}

public void run(){
//Here, you simply want to loop for the entier list (max 40)
for(Req r: theList){
r.sendRequest()
)
}

public statc void main(String args[]){
//Create an instance of your thread:
MyClass t = new MyClass(ReqList<..>());

//Now that you have your thread, simply do the following:

while(true){
t = new MyClass( (insert your new list));
t.start();
try{
Thread.sleep(1000);
}catch(Exception e){

}
)



}
}

那里有

关于java - 根据时间限制运行线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25916745/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com