要求:当一本书第一次生成时,另起一个分支。
要求:
1、多线处理排版(借鉴极速成书接口)。
2、对每一篇博客的排版数据,进行批量入库。
================
1 第一次成书的时候使用 此时podbook pod册子数据都没有
2 利用多线程对所有的list<数据> 排版
3 代码
public void ThreadealBlogBook(List<BlogArticle> blogArticleList, final float rowh1, final float wordsize1, final BlogBook blogBook1) {
long start = System.currentTimeMillis();
final Iterator<BlogArticle> iterarticle = blogArticleList.iterator();
ExecutorService thPool = Executors.newFixedThreadPool(5); //开启有5个线程的线程池
// 这种方式不分线程的顺序,先结束的先返回 comPool.take() 是阻塞方法且容量为一与call()方法的组合
CompletionService<PodBook> comPool = new ExecutorCompletionService<PodBook>(thPool);
while (iterarticle.hasNext()) { //每一个提交的任务
final BlogArticle blogArticle = iterarticle.next();
if (null != blogArticle) {
comPool.submit(new Callable<PodBook>() {
@Override
public PodBook call() {
PodBook podbook = new PodBook();
podbook = PodBook.dao.generateBlogBookSGSPThread(blogArticle, rowh1, wordsize1, "10", blogBook1);
return podbook;
}
});
}
}
List<PodBook> podbooks = new ArrayList<PodBook>();
Iterator iter = blogArticleList.iterator();
while (iter.hasNext()) {
try {
iter.next();
System.out.println("size--");
podbooks.add(comPool.take().get()); //上面的代码与此处是组合使用的
} catch (Exception e) {
e.printStackTrace();
}
}
thPool.shutdown();
//jfinal 集合对象的批量处理
Db.batch("insert into pod_book (booktemplateid,kaibentempletid,width,height,contenttype,levelid1,"
+ "levelid2,creatime,lastpodxml,totalpage,lastupdatetime,wordcount,title_name,bookhashcode) "
+ "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
"booktemplateid,kaibentempletid,width,height,contenttype,levelid1,levelid2,creatime,lastpodxml,totalpage,lastupdatetime,wordcount,title_name,bookhashcode",
podbooks, 10);
System.out.println("use + ");
System.out.println(System.currentTimeMillis() - start);
}
要求:
1、多线处理排版(借鉴极速成书接口)。
2、对每一篇博客的排版数据,进行批量入库。
================
1 第一次成书的时候使用 此时podbook pod册子数据都没有
2 利用多线程对所有的list<数据> 排版
3 代码
public void ThreadealBlogBook(List<BlogArticle> blogArticleList, final float rowh1, final float wordsize1, final BlogBook blogBook1) {
long start = System.currentTimeMillis();
final Iterator<BlogArticle> iterarticle = blogArticleList.iterator();
ExecutorService thPool = Executors.newFixedThreadPool(5); //开启有5个线程的线程池
// 这种方式不分线程的顺序,先结束的先返回 comPool.take() 是阻塞方法且容量为一与call()方法的组合
CompletionService<PodBook> comPool = new ExecutorCompletionService<PodBook>(thPool);
while (iterarticle.hasNext()) { //每一个提交的任务
final BlogArticle blogArticle = iterarticle.next();
if (null != blogArticle) {
comPool.submit(new Callable<PodBook>() {
@Override
public PodBook call() {
PodBook podbook = new PodBook();
podbook = PodBook.dao.generateBlogBookSGSPThread(blogArticle, rowh1, wordsize1, "10", blogBook1);
return podbook;
}
});
}
}
List<PodBook> podbooks = new ArrayList<PodBook>();
Iterator iter = blogArticleList.iterator();
while (iter.hasNext()) {
try {
iter.next();
System.out.println("size--");
podbooks.add(comPool.take().get()); //上面的代码与此处是组合使用的
} catch (Exception e) {
e.printStackTrace();
}
}
thPool.shutdown();
//jfinal 集合对象的批量处理
Db.batch("insert into pod_book (booktemplateid,kaibentempletid,width,height,contenttype,levelid1,"
+ "levelid2,creatime,lastpodxml,totalpage,lastupdatetime,wordcount,title_name,bookhashcode) "
+ "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
"booktemplateid,kaibentempletid,width,height,contenttype,levelid1,levelid2,creatime,lastpodxml,totalpage,lastupdatetime,wordcount,title_name,bookhashcode",
podbooks, 10);
System.out.println("use + ");
System.out.println(System.currentTimeMillis() - start);
}