博客书多线程排版

多线程处理稀缺资源并返回数据
// +++多线程处理公共方法
public void ThreadealBlogBook(List blogArticleList, final float rowh1, final float wordsize1, final BlogBook blogBook1) {

    long start = System.currentTimeMillis();
    final Iterator<BlogArticle> iterarticle = blogArticleList.iterator();

    int current = 0;
    ExecutorService thPool = Executors.newFixedThreadPool(5);
    // 这种方式不分线程的顺序,先结束的先返回
    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();

    ExecutorService exec = Executors.newFixedThreadPool(5); 
    final Iterator<BlogArticle> iterarticle1 = blogArticleList.iterator();
    ArrayList<Future<PodBook>>  results = new ArrayList<Future<PodBook>>();  
        while (iterarticle1.hasNext()) {
            final BlogArticle blogArticle = iterarticle1.next();
            results.add(exec.submit(new Callable<PodBook>() {
            @Override
            public PodBook call() {
                PodBook podbook = new PodBook();
                podbook = PodBook.dao.generateBlogBookSGSPThread(blogArticle, rowh1, wordsize1, "10", blogBook1);
                return podbook;
            }
        }));
    }  
    Vector<PodBook> podbooks = new Vector<PodBook>();
    for(Future<PodBook> fs : results){  
        try{  
            System.out.println(current++);
            podbooks.add(fs.get());//可以调用很多方法,包括是否工作等等  
        }catch(Exception e){  
            e.printStackTrace();  
        }finally{  
            exec.shutdown();  
        }  
    }  

    System.out.println("piaban + ");
    System.out.println(System.currentTimeMillis() - start);

    System.out.println("total" + current);

    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);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值