Java 根据时间限制运行线程
我希望每秒最多启动40个http请求,在1秒后,我希望它从自己的队列(如threadpooltaskexecutor的阻塞队列)运行另40个http请求。我正在寻找一个执行器或线程池实现这个要求 有什么建议吗 Thx 阿里 编辑:由于明显的原因,固定利率效率不高。随着队列项目一个接一个地启动,队列后面的项目将刚刚启动,但已经启动一段时间的项目可能会完成Java 根据时间限制运行线程,java,multithreading,scheduled-tasks,threadpool,Java,Multithreading,Scheduled Tasks,Threadpool,我希望每秒最多启动40个http请求,在1秒后,我希望它从自己的队列(如threadpooltaskexecutor的阻塞队列)运行另40个http请求。我正在寻找一个执行器或线程池实现这个要求 有什么建议吗 Thx 阿里 编辑:由于明显的原因,固定利率效率不高。随着队列项目一个接一个地启动,队列后面的项目将刚刚启动,但已经启动一段时间的项目可能会完成 额外编辑:问题是在一秒钟内只调用40个请求,而不是激活max 40。另一秒可以是80,但在1秒内应该只有40个新创建的连接。一种方法是使用另一种
额外编辑:问题是在一秒钟内只调用40个请求,而不是激活max 40。另一秒可以是80,但在1秒内应该只有40个新创建的连接。一种方法是使用另一种体系结构,这将使该过程更加容易 1) 创建一个实现runnable的线程类。 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){
}
)
}
}
类MyClass扩展线程
私人阵列列表;
公共MyClass(数组列表){
this.theList=theList;
}
公开募捐{
//在这里,您只需要循环查找entier列表(最大40个)
对于(请求:列表){
r、 sendRequest()
)
}
公用statc void main(字符串参数[]){
//创建线程的实例:
MyClass t=新的MyClass(ReqList());
//现在您有了线程,只需执行以下操作:
while(true){
t=新的MyClass((插入新列表));
t、 start();
试一试{
睡眠(1000);
}捕获(例外e){
}
)
}
}
在这里,您有了它首先定义一个实现Callable的类,它将执行线程的处理:
class MyClass implements Callable<String>
{
/**
* Consider this as a new Thread.
*/
@Override
public String call()
{
//Treatment...
return "OK"; //Return whatever the thread's result you want to be then you can access it and do the desired treatment.
}
}
类MyClass实现可调用
{
/**
*将此视为一个新线程。
*/
@凌驾
公共字符串调用()
{
//治疗。。。
return“OK”;//返回您想要的线程结果,然后您可以访问它并执行所需的处理。
}
}
下一步是在我的示例中创建一个ExecutorService,一个线程池并抛出一些任务
int nbThreadToStart = 40;
ExecutorService executor = Executors.newFixedThreadPool(/* Your thread pool limit */);
List<Future<String>> allTasks = new ArrayList<Future<String>>(/* Specify a number here if you want to limit your thread pool */);
for(int i = 0; i < 10; i++)//Number of iteration you want
{
for(int i = 0; i < nbThreadToStart; i++)
{
try
{
allTasks.add(executor.submit(new MyClass()));
}
catch(Exception e)
{
e.printStackTrace();
}
}
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
e.printStackTrace();
}
}
executor.shutdown();
//You can then access all your thread(Tasks) and see if they terminated and even add a timeout :
try
{
for(Future<String> task : allTasks)
task.get(60, TimeUnit.SECONDS);//Timeout of 1 seconds. The get will return what you specified in the call method.
}
catch (TimeOutException te)
{
...
}
catch(InterruptedException ie)
{
...
}
catch(ExecutionException ee)
{
...
}
int-nbThreadToStart=40;
ExecutorService executor=Executors.newFixedThreadPool(/*您的线程池限制*/);
List allTasks=new ArrayList(/*如果要限制线程池,请在此处指定一个数字*/);
for(int i=0;i<10;i++)//所需的迭代次数
{
对于(int i=0;i
我不确定您真正想要的是什么,但我认为您应该使用线程池处理多线程,特别是如果您计划接收大量请求以避免任何不必要的内存泄漏等
如果我的示例不够清晰,请注意ExexutorService、Future等提供的许多其他方法在处理线程时非常有用
看看这个:
这就是我的建议。一般来说,你想安排一些任务,或者在固定的时间间隔后调用它们吗?我认为问题很清楚。固定时间间隔是一个解决方案,但效率不高,因为你会说每25毫秒我需要启动一个,但正如你所看到的,我的第40个线程将在大约1秒后启动。但是w对于批量启动,这不会发生。不,不正确。您只是忘记了将响应返回给调用者!而且也没有线程队列,或者限制为40等。我知道我已经检查了spring、java core等上的类型。您的解决方案很接近,但不灵活。迭代计数应该是不必要的,因为可能有传入的cal这样一来,名单就不固定了