BlockQueue使用之简单应用
2015-12-22 00:00
453 查看
摘要: 最近在搞爬虫,其中有一项统计功能不仅让我计数,还要我存一下URL,首先是无语,但是还是要实现
然后最后就想到了将URL临时存储到队列中,然后再有线程读取写入Log,说的好像挺简单但是
对于我这种小菜,BlockQueue这里堵了好几次.........
当然试了好几种,包括用add方法抛异常,用offer方法判断BlockQueue有没有满,但是感觉没有从根本上解决问题,于是就用了下面的方法,设置队列的容量是1000,当达到900的时候,就启用日志线程读取。
/**
* 存储计数的信息
* @throws InterruptedException
*/
@Override
public void add(String counter, String url) throws Exception {
if( true == urlInputLogFile ){
BlockingQueue<String> blockingQueue = countersMap.get(counter);
if(null == blockingQueue){
blockingQueue = new ArrayBlockingQueue<String>(1000);
blockingQueue.put(url);
countersMap.put(counter, blockingQueue);
this.add(counter);
}else{
this.add(counter);
if(blockingQueue.size()==900){ //容量是1000等到满去写会阻塞
String date = DateFormatUtils.format(new Date(), "yyyyMMdd");
String name = counter+date+".log";
File rootPath = new File(this.getClass().getResource("/").getPath());
File path = new File(rootPath, urlInputLogFilePath);
if(!path.exists()){
path.mkdirs();
}
File file = new File(path,name);
if(!file.exists()){
file.createNewFile();
}
redisUrlLog.setLogFile(file);
redisUrlLog.setQueue(blockingQueue);
blockingQueue.add(url); //这里要再有异常,只好抛了,要不然就堵啊
countersMap.put(counter, blockingQueue);
}else{
countersMap.put(counter, blockingQueue);
}
}
}else{
this.add(counter);
}
}
以上供大家一起学习有什么好想法,可以留言相互沟通。
然后最后就想到了将URL临时存储到队列中,然后再有线程读取写入Log,说的好像挺简单但是
对于我这种小菜,BlockQueue这里堵了好几次.........
当然试了好几种,包括用add方法抛异常,用offer方法判断BlockQueue有没有满,但是感觉没有从根本上解决问题,于是就用了下面的方法,设置队列的容量是1000,当达到900的时候,就启用日志线程读取。
/**
* 存储计数的信息
* @throws InterruptedException
*/
@Override
public void add(String counter, String url) throws Exception {
if( true == urlInputLogFile ){
BlockingQueue<String> blockingQueue = countersMap.get(counter);
if(null == blockingQueue){
blockingQueue = new ArrayBlockingQueue<String>(1000);
blockingQueue.put(url);
countersMap.put(counter, blockingQueue);
this.add(counter);
}else{
this.add(counter);
if(blockingQueue.size()==900){ //容量是1000等到满去写会阻塞
String date = DateFormatUtils.format(new Date(), "yyyyMMdd");
String name = counter+date+".log";
File rootPath = new File(this.getClass().getResource("/").getPath());
File path = new File(rootPath, urlInputLogFilePath);
if(!path.exists()){
path.mkdirs();
}
File file = new File(path,name);
if(!file.exists()){
file.createNewFile();
}
redisUrlLog.setLogFile(file);
redisUrlLog.setQueue(blockingQueue);
blockingQueue.add(url); //这里要再有异常,只好抛了,要不然就堵啊
countersMap.put(counter, blockingQueue);
}else{
countersMap.put(counter, blockingQueue);
}
}
}else{
this.add(counter);
}
}
以上供大家一起学习有什么好想法,可以留言相互沟通。
相关文章推荐
- UIScrollView,UITableView的简单使用
- Riemann积分和Lebesgue积分角度下一积分不等式的等号成立充要条件的研究
- MySQL提示:The server quit without updating PID file问题的解决办法
- 设置UILabel的行间距
- 关于很简单的设计模式,ui-dao-database,以及JavaBean和util工具类
- 笔记-引导页GuideActivity
- 子线程改变UI线程的方法
- UIScreen的scale属性
- LabVIEW如何将脚本插入Quick Drop
- dispatch_get_current_queue的废弃
- [uva] 10099 - The Tourist Guide
- NGUI 这一年填过的那些坑(不定时更新)
- Codeforces 597C Subsequences dp + 树状数组
- UILable的text设置中划线(删除线)
- Arduino - 多线程库SCoop应用 之 任务
- UIView基础篇
- UITableView tableFooterView autolayout自动计算高度
- Replication-Replication Distribution Subsystem: agent xxxxxx failed. Column names in each table must be unique
- UIDynamic简单总结
- createQuery is not valid without active transaction