多线程处理Future,主线程等待子线程完成后统一处理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/HUXU981598436/article/details/90446213

更多多线程知识请访问 www.itkc8.com

 

package com.cowboy.service;

import java.util.concurrent.*;

/**
 * @ClassName CommonThreadPool
 * @Description TODO
 * @Author hux
 * @Date 2019/5/22、15:40
 * @Version 1.0
 **/

public class CommonThreadPool {

    private static ExecutorService exec = new ThreadPoolExecutor(50, 100, 0L,
            TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(10000),
            new ThreadPoolExecutor.CallerRunsPolicy());

    public static void execute(Runnable command) {
        exec.execute(command);
    }

    /**
     * 子线程执行结束future.get()返回null,若没有执行完毕,主线程将会阻塞等待
     * @param command
     * @return
     */
    public static Future submit(Runnable command) {
        return exec.submit(command);
    }

    /**
     * 子线程中的返回值可以从返回的future中获取:future.get();
     * @param command
     * @return
     */
    public static Future submit(Callable command) {
        return exec.submit(command);
    }

    public static void shutdown(){
        exec.shutdown();
    }

}
package com.cowboy.service;

import com.cowboy.model.Article;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @ClassName ThreadTest
 * @Description TODO
 * @Author hux
 * @Date 2019/5/22、15:41
 * @Version 1.0
 **/

public class ThreadTest {

    private static final String URL = "https://blog.csdn.net/rx3oyuyi";//"https://blog.csdn.net/foruok";


    public static void main(String[] args) throws IOException {

        int pageNow = 1;
        int totalPage = getTotalPage();
        System.out.println("总页数:"+totalPage);

        List<Future> futureList = new ArrayList<>();
        for(pageNow = 1; pageNow <= totalPage; pageNow++) {
            int finalI = pageNow;
            Callable<List<Article>> task = () -> {
                List<Article> artitcleByPage = getArtitcleByPage(finalI);
                return artitcleByPage;
            };
            Future submit = CommonThreadPool.submit(task);
            futureList.add(submit);
        }

        //主线程处理其他工作,让子线程异步去执行.
        System.out.println("now waiting sub thread done.");
        //主线程其他工作完毕,等待子线程的结束, 调用future.get()系列的方法即可。
        try {
            for (Future future : futureList) {
                System.out.println(future.get());
            }
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        CommonThreadPool.shutdown();
    }

    /**
     * 获取总页数
     * @return
     */
    public static int getTotalPage(){
        Connection conn = Jsoup.connect(URL)
                .userAgent("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0")
                .timeout(8000)
                .method(Connection.Method.GET);
        Document doc = null;
        try {
            doc = conn.get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Element body = doc.body();
        int totalPage = 1;
        Elements scripts = body.getElementsByTag("script");
        Iterator it = scripts.iterator();
        while(it.hasNext()) {
            Element element = (Element)it.next();
            String text = element.data();
            if (text.contains("pageSize") && text.contains("listTotal")) {
                int i = text.indexOf("var pageSize = ");
                int i1 = text.indexOf("var listTotal = ");
                int i2 = text.indexOf("var pageQueryStr =");
                String pageSize = text.substring(i+15, i1);
                String listTotal = text.substring(i1+16, i2);
                double i3 = Double.parseDouble(pageSize.replace(";", "").trim());
                double i4 = Double.parseDouble(listTotal.replace(";", "").trim());
                double number = i4 / i3;
                totalPage = (int)Math.ceil(number);
                break;
            }
        }
        return totalPage;
    }

    /**
     * 分页读取
     * @param pageNow
     * @return
     * @throws IOException
     */
    public static List<Article> getArtitcleByPage(int pageNow) throws IOException {
        Connection conn = Jsoup.connect(URL + "/article/list/" + pageNow)
                .userAgent("Mozilla/5.0 (Windows NT 6.1; rv:47.0) Gecko/20100101 Firefox/47.")
                .timeout(8000)
                .method(Connection.Method.GET);
        Document doc = conn.get();
        Element body = doc.body();
        List<Article> resultList = new ArrayList<>();
        Elements articleList = body.getElementsByClass("article-item-box");
        for(Element article : articleList){
            Article articleEntity = new Article();
            Element linkNode = (article.select("div h4 a")).get(0);
            Element desptionNode = (article.getElementsByClass("content")).get(0);
            Element articleManageNode = (article.getElementsByClass("info-box")).get(0);
            articleEntity.setAddress(linkNode.attr("href"));
            articleEntity.setTitle(linkNode.text());
            articleEntity.setDesption(desptionNode.text());
            articleEntity.setTime(articleManageNode.getElementsByClass("date").text());
            articleEntity.setReadNum(articleManageNode.getElementsByClass("read-num").get(0).getElementsByClass("num").text());
            articleEntity.setCommentNum(articleManageNode.getElementsByClass("read-num").get(1).getElementsByClass("num").text());
            resultList.add(articleEntity);
        }
        return resultList;
    }


}

 

展开阅读全文

没有更多推荐了,返回首页