【发布时间】:2011-09-29 19:24:29
【问题描述】:
更新
我已经使用了FixedThreadPool。发生的情况是每个线程为一个站点打开一个连接。我想做的是异步的。
- 向服务器发送请求
- 无需等待第一个请求完成即可转到下一个请求
- 当请求建立后,通知另一个线程连接已建立并准备好下载。
我认为这将加快执行速度,因为将使用更少的线程来打开与当前性能相同或更多的连接。
在当前的方式中,每个线程都会等待一段时间,而无需等待连接建立。以这种新方式,它将始终有效。
问题
我想知道是否有一种方法可以只用一个线程打开与多个站点的连接。
这是因为我在做一个网络爬虫,我已经做了一个线程来打开一个连接,但是在一定数量的线程之后,这并没有帮助,因为处理器共享会增加很多。
我希望这可以加快下载页面的数量。有可能这样做吗?怎么样?
此代码打开一个连接并进行一些处理。它由打开连接的线程执行
/*
* Open connection to a server
*/
boolean openConnection(Link link) throws Exception {
//set the connection paramenters
HttpURLConnection conn = (HttpURLConnection) new URL(link.getOriginalURL().getURL()).openConnection();
conn.setRequestProperty("User-Agent", ROBOT_NAME);
conn.setInstanceFollowRedirects(true);
conn.setConnectTimeout(READ_TIMEOUT);
conn.setReadTimeout(READ_TIMEOUT);
link.setConnection(conn);
//open the connection
conn.connect();
//check the server answer
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
return false;
}
//analyse the URL of the redirected URL
urlAnalyzer.fillURL(link.getRedirectedURL(), getRedirectedURL(link.getConnection()));
return true;
}
这会执行连接打开器,每个线程都在一个线程中
/*
* Start the execution of the connection openers
*/
private void executeConnectionOpeners() {
LOGGER.info("Starting connection openners.");
/* Execution */
NameThreadFactory ntf = new NameThreadFactory("Connection Opener");
crawlerOpenerExecutor = Executors.newFixedThreadPool(nOpeners, ntf);
for (int i = 0; i < nOpeners; i++) {
crawlerOpenerExecutor.submit(new ConnectionOpener(this));
}
/* End of execution */
LOGGER.info(nOpeners + " connection openers created and running.");
}
【问题讨论】:
-
为什么不使用固定大小的线程池?
-
分享你的爬虫Thread代码块,社区可以提供更好的反馈
标签: java http connection web-crawler