【发布时间】:2019-06-08 09:12:21
【问题描述】:
我需要点击端点超过 1000 次才能从网站获取一些数据。所以我阅读了一些教程来使用 Multi Threading 来实现它。但一次我只想在同一方法上使用 13 个线程。
所以基本上我使用 ExecutorService 一次运行 13 个线程:
ExecutorService threadPool = Executors.newFixedThreadPool(13);
for (int itLocation = 0; itLocation < locationList.size(); itLocation++) {
//some code like
ScraperService obj = new ScraperService(threadName,url)
threadPool.submit(obj);
}
threadPool.shutdown();
我的名为 ScraperService 的 Groovy 类正在实现 Runnable 接口。
@Override
void run() {
println("In run method...................")
try {
Thread.sleep(5000);
someMethod()
} catch (InterruptedException e) {
e.printStackTrace();
}
}
问题:
我的问题是我的ExecutorService.submit(obj) 和ExecutorService.execute(obj) 没有调用我的Runnable 接口的run() 方法。
在 Groovy/Grails 中:
还有一个执行器插件Executor Plugin in grails,但我没有找到任何合适的示例如何使用它。
【问题讨论】:
-
你为什么不使用
threadPool.invokeAll(tasks);?但就您而言,我认为您必须致电threadPool.awaitTermination() -
我现在不关心 threadPool.invokeAll(tasks);
标签: multithreading grails groovy threadpool executorservice