首页 > 编程语言 > 详细

多线程代码案例

时间:2021-04-20 14:11:02      阅读:35      评论:0      收藏:0      [点我收藏+]

案例

package com.zxwa.ntmss.img2text;

import com.alibaba.fastjson.JSONObject;
import com.zxwa.ntmss.img2text.utils.FileUtils;
import com.zxwa.ntmss.process.common.util.sql.JdbcUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import java.net.HttpURLConnection;
import java.net.URL;
import java.text.MessageFormat;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.zxwa.ntmss.img2text.thread.Img2TextThread.readString;

public class OtherNtmssV3ContentImgProcesser3 {

    private static JdbcTemplate zxCrawlerDbJdbcTemplate = JdbcUtils.getZxCrawlerDbJdbcTemplate();

    private static Logger LOG = LoggerFactory.getLogger(OtherNtmssV3ContentImgProcesser3.class);


    public static void main(String[] args) throws InterruptedException {
        while (true) {
            int tableNum = 10;
            ConcurrentLinkedQueue<Map<String, Object>> queue = new ConcurrentLinkedQueue<>();
            CountDownLatch count = new CountDownLatch(tableNum);
            List<Map<String, Object>> mapList = zxCrawlerDbJdbcTemplate.queryForList("SELECT * FROM `cxyxproduct` s WHERE s.text is NULL LIMIT 100;");
            queue.addAll(mapList);
            System.out.println("-----------------------------------开饭了-----------------------------------");
            long start = System.currentTimeMillis();
            ExecutorService executorService = Executors.newFixedThreadPool(tableNum);
            for (int i = 0; i < tableNum; i++) {
                executorService.submit(new Dinner("00" + (i + 1), queue, count));
            }
            //计数器等待,知道队列为空(所有人吃完)
            count.await();
            long time = System.currentTimeMillis() - start;
            System.out.println("-----------------------------------所有人已经吃完-----------------------------------");
            System.out.println("共耗时:" + time);
            //停止线程池
//            executorService.shutdown();
        }
    }
 private static class Dinner implements Runnable {
        private String name;
        private ConcurrentLinkedQueue<Map<String, Object>> queue;
        private CountDownLatch count;

        public Dinner(String name, ConcurrentLinkedQueue<Map<String, Object>> queue, CountDownLatch count) {
            this.name = name;
            this.queue = queue;
            this.count = count;
        }

        @Override
        public void run() {
            while (!queue.isEmpty()) {
                try {
                    Map<String, Object> map = queue.poll();
                    StringBuilder builder = new StringBuilder();
                    Object productImg = map.get("productimg");
                    if (productImg != null) {
                        String strImg = productImg.toString();
                        String[] split = strImg.split(",");
                        for (String url : split) {
                            String fileUrl = MessageFormat.format("http://", FileUtils.formatUrl(url), "4", map.get("id"));
                            URL urlReal = new URL(fileUrl);
                            //打开链接
                            HttpURLConnection conn = (HttpURLConnection) urlReal.openConnection();
                            //设置请求方式为"GET"
                            conn.setRequestMethod("GET");
                            //超时响应时间为5秒
                            conn.setConnectTimeout(15 * 1000);
                            String response = readString(conn.getInputStream());
                            String text = JSONObject.parseObject(response).getString("text");
                            if (text == null) {
                                text = "";
                            }
                            builder.append(text).append("#");
                        }
                    }
                 String str = builder.toString();
                    String id = map.get("id").toString();
                    zxCrawlerDbJdbcTemplate.update("UPDATE `spider_data_15f`.`cxyxproduct` s SET s.text=?,s.productimg_new=s.productimg  WHERE (s.id=?);", str, id);
                    LOG.info(id + "\t" + str);
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
            }
            count.countDown();//计数器-1
        }
    }
}

 

多线程代码案例

原文:https://www.cnblogs.com/luweiweicode/p/14680023.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!