作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有要求打端点超过 1000 次 从网站获取一些数据。所以我读了一些教程来使用多线程 去实现它。但一次我只想使用 13 个线程 用同样的方法 .
所以基本上我正在使用执行人服务 一次运行 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();
@Override
void run() {
println("In run method...................")
try {
Thread.sleep(5000);
someMethod()
} catch (InterruptedException e) {
e.printStackTrace();
}
}
ExecutorService.submit(obj)
和
ExecutorService.execute(obj)
没有调用我的 Runnable 接口(interface)的 run() 方法。
最佳答案
GPars 非常适合这种类型的事情。
您的 ScraperService 可以只负责处理像下面这样的抓取数据,或者也可以获取它,无论如何。
import groovyx.gpars.GParsPool
def theEndpoint = 'http://www.bbc.co.uk'
def scraperService
GParsPool.withPool( 13 ) {
(1..1000).eachParallel {
scraperService.scrape theEndpoint.toURL().text
}
}
关于multithreading - 如何在 groovy 中使用多线程访问 1000 个端点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54180536/
我是一名优秀的程序员,十分优秀!