我有一项任务,我希望并行运行500次,如果我想只运行一次我会做这样的事情
Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(
taskProvider.get(),
0,
100,
TimeUnit.MILLISECONDS
);
我想运行500个任务(高IO),我从任务提供程序以固定速率并行运行,有没有办法在不创建500个执行程序或在while循环中提交500个任务的情况下执行此操作?
编辑:我知道我可以创建一个大小为500的线程池,如果我这样做,我将不得不提交500个任务(?)来并行运行它们,我看是否有办法在不提交的情况下执行它类似的任务,因为基本上他们有相同的内容,我也不想同时启动所有这些,我想每100毫秒开始50。答案 0 :(得分:1)
您必须手动执行此操作:
void submitTaskChunks(int chunkCount, int chunkSize, long delayMillis) throws InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < chunkCount; i++) {
List<Callable<Void>> taskChunk = IntStream.range(0, chunkSize)
.mapToObj(k -> taskProvider.get())
.collect(Collectors.toList());
executorService.invokeAll(taskChunk);
Thread.sleep(delayMillis);
}
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.MINUTES);
}
每100毫秒提交50次任务10次:
submitTaskChunks(10, 50, 100);
请注意,taskProvider.get应返回Callable<Void>
。
答案 1 :(得分:0)
您可以创建大小为500的任务列表并使用
<%@ Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
jobscheduler.Start();
// Code that runs on application startup
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
</script>