ArrayBlockingQueue 阻塞队列 生产者 与消费者案例
package com.originalityTest; import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.yqm.nlp.cn.seg.ISegTagger;
import org.yqm.nlp.cn.seg.impl.CharNgramSegTagger; import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;
import com.originalityTest.Test.Consumer;
import com.originalityTest.Test.Producer; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import us.codecraft.background.entity.KeywordDetailed;
import us.codecraft.background.solr.SolrService;
import us.codecraft.background.solr.VSMTextSimilarity;
import us.codecraft.webmagic.main.CollectInterface;
import us.codecraft.webmagic.main.testMain;
import us.codecraft.webmagic.model.samples.iask.IaskQuestionModel;
import us.codecraft.webmagic.utils.HttpUtils;
import us.codecraft.webmagic.utils.MongoUtils; /**
*
*-----------------------------------------------------------------------------
* <br>Copyright (c) 2018 深圳问我时代科技有限公司
*
* <p>跑360采集数据跑SEO质量得分 </p>
*
* @project name : webmagic-samples
* @package name : com.originalityTest
* @file name : RunJob.java
* @author : flm
* @date : 2018年10月29日 <br>
*
*-----------------------------------------------------------------------------
*/
public class RunJob { protected static String host = "192.168.1.90"; //192.168.9.40:30000 protected static int port = 30000;
protected static String dbname = "5118baiduzhidao";
protected static DB mongoDB = null;
protected static DB mongoDBURL = null;
protected static boolean isTestFlag = false;
protected static int initDate = 1000*60*10; private int queueSize = 10000000;
private ArrayBlockingQueue<BasicDBObject> queue = new ArrayBlockingQueue<BasicDBObject>(queueSize); static {
MongoClientOptions.Builder buide = new MongoClientOptions.Builder();
buide.connectionsPerHost(100);// 与目标数据库可以建立的最大链接数
buide.connectTimeout(1000 * 60 * 20);// 与数据库建立链接的超时时间
buide.maxWaitTime(100 * 60 * 5);// 一个线程成功获取到一个可用数据库之前的最大等待时间
buide.threadsAllowedToBlockForConnectionMultiplier(100);
buide.maxConnectionIdleTime(0);
buide.maxConnectionLifeTime(0);
buide.socketTimeout(0);
buide.socketKeepAlive(true);
MongoClientOptions myOptions = buide.build();
try {
MongoClient mongoClient = new MongoClient(new ServerAddress(host, port), myOptions);
mongoDB = mongoClient.getDB(dbname);
mongoDBURL = mongoClient.getDB("seo_keyword");
} catch (UnknownHostException e) {
e.printStackTrace();
System.exit(0);
} }
/**
* 手动执行方法
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
RunJob runJob = new RunJob();
Producer producer = runJob.new Producer();
Consumer consumer1 = runJob.new Consumer(1);
Consumer consumer2 = runJob.new Consumer(2);
Consumer consumer3 = runJob.new Consumer(3);
Consumer consumer4 = runJob.new Consumer(4);
Consumer consumer5 = runJob.new Consumer(5);
Consumer consumer6 = runJob.new Consumer(6);
Consumer consumer7 = runJob.new Consumer(7);
Consumer consumer8 = runJob.new Consumer(8);
Consumer consumer9 = runJob.new Consumer(9);
Consumer consumer10 = runJob.new Consumer(10);
// 生产数据
producer.start(); // 跑数据原创度
consumer1.start();
consumer2.start();
consumer3.start();
consumer4.start();
consumer5.start();
consumer6.start();
consumer7.start();
consumer8.start();
consumer9.start();
consumer10.start();
} class Consumer extends Thread{
int i;
public Consumer(int i){
this.i = i;
}
@Override
public void run() {
consume();
} private void consume() {
while(true){
try {
System.out.println("队列获取 队列i:"+i);
DBCollection collQuestion = mongoDB.getCollection("soQA");
DBCollection collection = mongoDBURL.getCollection("domain"); BasicDBObject d = queue.take(); List<DBObject> answers= (List<DBObject>) d.get("answers");
String questionTxt = d.getString("title") + d.getString("quest"); String questionTitle = "";
if(d.getString("title")!=null&&d.getString("title")!=""){
questionTitle = d.getString("title");
}else{
questionTitle = d.getString("quest");
} float score = 0F; float answerLenOriginality = OriginalityUtitls.getAnswerLenOriginality(answers);
float answerSizeOriginality = OriginalityUtitls.getAnswerrSizeOriginality(answers);
float goodOriginality = OriginalityUtitls.getGoodOriginality(answers);
float questionOriginality = OriginalityUtitls.getQuestionOriginality(questionTxt, answers);
float titleOriginality = OriginalityUtitls.getTitleOriginality(questionTitle);
float wenwoOriginality = OriginalityUtitls.getWenwoOriginality(questionTitle, collection); score += Float.valueOf(questionOriginality*0.3+"");
score += Float.valueOf(answerLenOriginality*0.2+"");
score += Float.valueOf(answerSizeOriginality*0.1+"");
score += Float.valueOf(goodOriginality*0.05+"");
score += Float.valueOf(titleOriginality*0.2+"");
score += Float.valueOf(titleOriginality*0.2+"");
score += Float.valueOf(wenwoOriginality*0.1+"");
System.out.println("score :"+score); d.put("originality", score);
d.put("run", 0);
collQuestion.save(d);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} class Producer extends Thread{
@Override
public void run() {
produce();
} private void produce() {
try {
DBCollection collQuestion = mongoDB.getCollection("soQA"); DBObject idQuery = new BasicDBObject();
idQuery.put("state",1);
DBCursor lists = collQuestion.find(idQuery);
lists.addOption(com.mongodb.Bytes.QUERYOPTION_NOTIMEOUT);
int i = 0;
while(lists.hasNext()){
BasicDBObject d = (BasicDBObject)lists.next();
queue.put(d);
System.out.println((++i)+"条记录 ,向队列取中插入一个元素,队列剩余空间:"+(queueSize-queue.size())); try {
Thread.sleep(500); // 控制生产速度,防止队列满
} catch (Exception e) {
System.err.println("Thread.sleep.....");
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} }
ArrayBlockingQueue 阻塞队列 生产者 与消费者案例的更多相关文章
- 【JUC】阻塞队列&生产者和消费者
阻塞队列 线程1往阻塞队列添加元素[生产者] 线程2从阻塞队列取出元素[消费者] 当队列空时,获取元素的操作会被阻塞 当队列满时,添加元素的操作会被阻塞 阻塞队列的优势:在多线程领域,发生阻塞时,线程 ...
- java学习之生产者和消费者案例
package com.gh.thread; /** * 生产者和消费者案例 * wait和sleep的区别 * wait不让出监视器锁,sleep让出监视器的锁 * @author ganhang ...
- GUC-13 生产者和消费者案例-旧
/* * 生产者和消费者案例 */ public class TestProductorAndConsumer { public static void main(String[] args) { C ...
- 同步锁Lock & 生产者和消费者案例
显示锁 Lock ①在 Java 5.0 之前,协调共享对象的访问时可以使用的机 制只有 synchronized 和 volatile . Java 5.0 后增加了一些 新的机制,但并不是一种替代 ...
- Condition对象以及ArrayBlockingQueue阻塞队列的实现(使用Condition在队满时让生产者线程等待, 在队空时让消费者线程等待)
Condition对象 一).Condition的定义 Condition对象:与锁关联,协调多线程间的复杂协作. 获取与锁绑定的Condition对象: Lock lock = new Reentr ...
- 10 阻塞队列 & 生产者-消费者模式
原文:http://www.cnblogs.com/dolphin0520/p/3932906.html 在前面我们接触的队列都是非阻塞队列,比如PriorityQueue.LinkedList(Li ...
- 并发容器之阻塞队列DelayQueue的使用案例及源码分析
原文连接:(http://www.studyshare.cn/blog-front//blog/details/1167/0 ) 一.队列及阻塞队列概念 1.队列:是一种特殊线性表,特殊之处在于操作是 ...
- BlockingQueue 阻塞队列(生产/消费者队列)
1:BlockingQueue的继承关系 java.util.concurrent 包里的 BlockingQueue是一个接口, 继承Queue接口,Queue接口继承 Collection Blo ...
- 守护进程,互斥锁,IPC,队列,生产者与消费者模型
小知识点:在子进程中不能使用input输入! 一.守护进程 守护进程表示一个进程b 守护另一个进程a 当被守护的进程结束后,那么守护进程b也跟着结束了 应用场景:之所以开子进程,是为了帮助主进程完成某 ...
随机推荐
- PHP开发——数据类型
概述 l 变量就是一个容器,变量本身并没有类型,变量的类型解决值的类型. l PHP和JS都属于弱类型语言,变量在运行过程中,类型是可以变的.但是,Java不可以. l 标量(基本)数据类型:字 ...
- vnc 搭建 转
这里要注意,关闭selinux setenforce 0 原文地址: http://www.linuxidc.com/Linux/2015-04/116725.htm 这是一个关于怎样在你的 Cent ...
- Array of Doubled Pairs LT954
Given an array of integers A with even length, return true if and only if it is possible to reorder ...
- SQL中GROUP BY语句与HAVING语句的使用
GROUP BY语句,经过研究和练习,终于明白如何使用了,在此记录一下同时添加了一个自己举的小例子,通过写这篇文章来加深下自己学习的效果,还能和大家分享下,同时也方便以后查阅,一举多得 一.GROUP ...
- UVA 10100 Longest Match
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=13&p ...
- centos7 安装maven
进入指定目录 cd /usr/local/src/ 下载maven 包 wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.1.1/bin ...
- JavaScript:原生模拟$.ajax以及jsonp
现实项目中,常常会用到请求,但是在考虑低版本的浏览器时,promise相关的axios,fetch这类第三方库的支持率就不那么好了,再考虑到最大的一个问题,跨域,更是让人头痛,虽然也有fetch-js ...
- Win7 VS2015 NASM汇编语言环境配置
参考了以下两个博客文章 http://blog.csdn.net/x356982611/article/details/51260841 http://www.cnblogs.com/antonioz ...
- ubuntu 14.04下搭建esp32开发环境
esp32是乐鑫出品的一款集成了wifi和蓝牙的集成模块,板上自带两个哈佛结构的Xtensa LX6 CPU双核处理器,本文主要讲解如何在linux下搭建其编译开发环境. 首先ctrl+alt+t打开 ...
- zookeeper配置文件共享中心
最近频繁的系统上线,每次打包都要把配置文件替换为正式环境的配置文件,虽然说就是复制粘贴的事,架不住文件杂乱,而且多. 期初的想法是有没有办法将配置文件与系统隔离开来,这样在更新时候,就只需要更新代码部 ...