我正在开发必须发送http请求的应用程序。应用程序必须在 1 天内为 100 000 个用户发送 5 个请求。我使用 spring mvc,我想使用线程来执行此 http 请求
for(int j = 0; j < 100; j++){
for(int i = 0; i < 5000; i++){
OrderActionThread thread = new OrderActionThread();
thread.start();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
OrderActionThread 发出 http 请求
我不知道我应该使用什么来完成这个任务...我已经阅读了有关 Spring 集成的内容,但我不确定我应该使用它。我该如何决定这个任务?
最佳答案
不要直接使用Thread
,上面的循环将创建 5 000 000 个线程,您很可能会耗尽内存,线程池最适合如此大量的线程。
因为你想每天安排它,所以我建议使用Executors.newScheduledThreadPool
:
final ScheduledExecutorService pool = Executors.newScheduledThreadPool(10);
for(int j = 0; j < 100; j++){
for(int i = 0; i < 5000; i++){
pool.scheduleAtFixedRate(new OrderActionThread(), 0, 1, TimeUnit.DAYS);
}
}
上述池将使用 10
线程(您可以增加该数量),并将安排所有 5 000 000 个任务每 24 小时重复一次(1
TimeUnit .DAYS
)。
关于java spring无尽的并发工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37767553/