【问题标题】:java 9 multithreaded HTTP client doesn't work correctlyjava 9 多线程 HTTP 客户端无法正常工作
【发布时间】:2023-04-02 09:49:01
【问题描述】:

我正在尝试将以下顺序代码转换为多线程代码,但结果对我来说听起来不合理。

package com.net;

import jdk.incubator.http.HttpClient;
import jdk.incubator.http.HttpRequest;
import jdk.incubator.http.HttpResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class Req {

    HttpClient client = HttpClient.newHttpClient();

    private String getResource(String someUrl) {
        String body = "";
        try {
            URI url = new URI(someUrl);
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(url).GET().build();
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandler.asString());
            body = response.body();
        } catch (URISyntaxException e) {
            System.out.println("URL " + someUrl + "is not valid");
        } catch (IOException | InterruptedException e) {
            System.out.println(e.getMessage());
        }
        return body;
    }


    public static void main(String[] args){

        String[] topIranianSites = {
                "https://www.aparat.com/",
                "http://www.varzesh3.com/",
                "http://namnak.com/",
                "http://www.telewebion.com/",
                "https://divar.ir/",
                "https://www.ninisite.com/",
                "https://www.blogfa.com/",
                "http://www.namasha.com/",
                "http://www.yjc.ir/"
        };

        Req singleThreadReq = new Req();
        float totalElapsedTime = 0F;

        for (String site : topIranianSites){
            long fetchStartTime = System.currentTimeMillis();
            String html = singleThreadReq.getResource(site);
            float elapsed = (float) (System.currentTimeMillis() - fetchStartTime) / 1000;

            Document doc = Jsoup.parse(html);
            System.out.println("It took " + elapsed + " seconds to fetch " + site + " with title " + doc.title());
            totalElapsedTime += elapsed;
        }

        System.out.println("Total Elapsed Time: " + totalElapsedTime + "\nTotal Number of sites: " + topIranianSites.length);

    }
}

这是输出

WARNING: Using incubator modules: jdk.incubator.httpclient
It took 2.622 seconds to fetch https://www.aparat.com/ with title آپارات - سرویس اشتراک ویدیو
It took 0.455 seconds to fetch http://www.varzesh3.com/ with title 
It took 0.521 seconds to fetch http://namnak.com/ with title نمناک
It took 2.172 seconds to fetch http://www.telewebion.com/ with title تلوبیون | مرجع پخش زنده و دانلود فیلم ، سریال و سایر برنامه های تلویزیون
General SSLEngine problem
It took 0.229 seconds to fetch https://divar.ir/ with title 
It took 1.769 seconds to fetch https://www.ninisite.com/ with title نی نی سایت | راهنمای بارداری و بچه داری
Received fatal alert: handshake_failure
It took 0.382 seconds to fetch https://www.blogfa.com/ with title 
It took 2.641 seconds to fetch http://www.namasha.com/ with title نماشا - سرویس رایگان اشتراک ویدیو
It took 0.503 seconds to fetch http://www.yjc.ir/ with title 
Total Elapsed Time: 11.294001
Total Number of sites: 9

从顺序输出中,我猜想正确的多线程代码应该需要大约 2.8 秒才能获取所有 9 个站点。但是我的多线程代码实现需要更多

package com.net;

import jdk.incubator.http.HttpClient;
import jdk.incubator.http.HttpRequest;
import jdk.incubator.http.HttpResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class ReqThreaded implements Runnable {

    class Site {
        String url;
        String title;
        float fetchTime;
    }

    private HttpClient client = HttpClient.newHttpClient();

    private Thread[] threadPool;
    private String[] rawSites;
    private Site[] sitesArr;
    private int sitesDone = 0;
    long startTime = System.currentTimeMillis();
    float totalElapsed = 0F;

    public ReqThreaded(String[] sites) {
        threadPool = new Thread[sites.length];
        sitesArr = new Site[sites.length];
        rawSites = sites;

        for (int i = 0; i < sites.length; i++) {
            startThread(i);
        }

        while (sitesDone < sites.length) {
            try {
                Thread.sleep(1000);
                totalElapsed = (float) (System.currentTimeMillis() - startTime) / 1000;
                System.out.print("\rElapsed time: " + totalElapsed + "Sites Done: " + sitesDone);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println("\n\nStatistics:\n\n");

        for (Site someSite : sitesArr) {
            System.out.println("URL " + someSite.url + "\nTitle: " + someSite.title + "\nFetch Time: " + someSite.fetchTime + "\n\n");
        }

    }

    private void startThread(int i) {
        if (threadPool[i] == null) {
            threadPool[i] = new Thread(this);
            threadPool[i].start();
        }
    }


    private String getResource(String someUrl) {
        String body = "";
        try {
            URI url = new URI(someUrl);
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(url).GET().build();
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandler.asString());
            body = response.body();
        } catch (URISyntaxException e) {
            System.out.println("URL " + someUrl + "is not valid");
        } catch (IOException | InterruptedException e) {
            System.out.println(e.getMessage());
        }
        return body;
    }


    @Override
    public void run() {
        Thread thisThread = Thread.currentThread();
        int sitesIndex = 0;

        for (int j = 0; j < threadPool.length; j++) {
            if (thisThread == threadPool[j]) {
                sitesIndex = j;
            }
        }
        long fetchStartTime = System.currentTimeMillis();
        String html = getResource(rawSites[sitesIndex]);
        float elapsed = (float) (System.currentTimeMillis() - fetchStartTime) / 1000;
        sitesDone++;
        Document doc = Jsoup.parse(html);
        sitesArr[sitesIndex] = new Site();
        sitesArr[sitesIndex].url = rawSites[sitesIndex];
        sitesArr[sitesIndex].title = doc.title();
        sitesArr[sitesIndex].fetchTime = elapsed;
    }

    public static void main(String[] args) {

        String[] topIranianSites = {
                "https://www.aparat.com/",
                "http://www.varzesh3.com/",
                "http://namnak.com/",
                "http://www.telewebion.com/",
                "https://divar.ir/",
                "https://www.ninisite.com/",
                "https://www.blogfa.com/",
                "http://www.namasha.com/",
                "http://www.yjc.ir/"
        };

        new ReqThreaded(topIranianSites);

    }
}

这是多线程代码的输出。每个 url 的总时间和获取时间似乎都不正确。我认为有些东西在这里阻塞或某种竞争条件。这里有什么问题?

WARNING: Using incubator modules: jdk.incubator.httpclient
General SSLEngine problem
Received fatal alert: handshake_failure
Elapsed time: 7.068Sites Done: 9

Statistics:


URL https://www.aparat.com/
Title: آپارات - سرویس اشتراک ویدیو
Fetch Time: 4.808


URL http://www.varzesh3.com/
Title: 
Fetch Time: 5.904


URL http://namnak.com/
Title: نمناک
Fetch Time: 1.056


URL http://www.telewebion.com/
Title: تلوبیون | مرجع پخش زنده و دانلود فیلم ، سریال و سایر برنامه های تلویزیون
Fetch Time: 6.569


URL https://divar.ir/
Title: 
Fetch Time: 0.53


URL https://www.ninisite.com/
Title: نی نی سایت | راهنمای بارداری و بچه داری
Fetch Time: 4.287


URL https://www.blogfa.com/
Title: 
Fetch Time: 0.767


URL http://www.namasha.com/
Title: نماشا - سرویس رایگان اشتراک ویدیو
Fetch Time: 4.539


URL http://www.yjc.ir/
Title: 
Fetch Time: 0.836

【问题讨论】:

  • 首先creating Threads in java is expensive,其次Thread.sleep(1000);会不必要地增加持续时间。
  • 您应该改用ExecutorService。问题中 90% 的代码是不必要的,而且可能是错误的。
  • @devpuh 我认为线程比处理阻塞操作的进程要轻得多,那么正确的方法是什么?像使用协程的异步等待是在 python 中处理此类工作的最佳方式。在java中最好的方法是什么?
  • @kayman 你能给我正确的答案吗?
  • @m.d 确实,java 线程比进程轻。但是每个线程都需要内存来存放它的堆栈,通常是 0.1 - 1 兆字节。所以正确的做法是在你有能力为它们消耗内存的情况下使用线程,然后切换到任务和线程池。

标签: java multithreading http2


【解决方案1】:

我修复了类 ReqThreaded 中的一些明显错误,例如不正确的同步,将方法 run() 移至类 Site,并使结果打印与单线程变体中的结果相同。未使用线程池,为每个请求创建了一个单独的线程。

结果如下:

Received fatal alert: handshake_failure
It took 0.263 seconds to fetch https://www.blogfa.com/ with title 
General SSLEngine problem
It took 0.491 seconds to fetch https://divar.ir/ with title 
It took 1.02 seconds to fetch http://www.yjc.ir/ with title 
It took 1.056 seconds to fetch http://www.telewebion.com/ with title تلوبیون | مرجع پخش زنده و دانلود فیلم ، سریال و سایر برنامه های تلویزیون
It took 1.262 seconds to fetch https://www.ninisite.com/ with title نی نی سایت | راهنمای بارداری و بچه داری
It took 1.411 seconds to fetch http://namnak.com/ with title نمناک
It took 1.608 seconds to fetch http://www.varzesh3.com/ with title ورزش سه :: صفحه اصلی
It took 2.221 seconds to fetch http://www.namasha.com/ with title نماشا - سرویس رایگان اشتراک ویدیو
It took 2.247 seconds to fetch https://www.aparat.com/ with title آپارات - سرویس اشتراک ویدیو
Elapsed time: 2.253
Sites Done: 9
Process finished with exit code 0

也就是说,总时间仅比从网站获得结果的最长时间稍长。

多线程工作!

修改后的代码如下:

import jdk.incubator.http.HttpClient;
import jdk.incubator.http.HttpRequest;
import jdk.incubator.http.HttpResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.CountDownLatch;

public class ReqThreaded {

class Site implements Runnable {
    final String url;
    String title;
    float fetchTime;

    Site(String url) {
        this.url = url;
    }

    @Override
    public void run() {
        long fetchStartTime = System.currentTimeMillis();
        String html = getResource(url);
        float elapsed = (float) (System.currentTimeMillis() - fetchStartTime) / 1000;
        Document doc = Jsoup.parse(html);
        title = doc.title();
        fetchTime = elapsed;
        System.out.println("It took " + fetchTime + " seconds to fetch " + url + " with title " + title);
        sitesDone.countDown();
    }
}

private HttpClient client = HttpClient.newHttpClient();

private CountDownLatch sitesDone;

public ReqThreaded(String[] sites) throws InterruptedException {
    int siteNumber = sites.length;
    sitesDone = new CountDownLatch(siteNumber);
    long startTime = System.currentTimeMillis();

    for (int i = 0; i < siteNumber; i++) {
        Runnable site = new Site(sites[i]);
        Thread thread = new Thread(site);
        thread.start();
    }

    sitesDone.await();
    float totalElapsed = (float) (System.currentTimeMillis() - startTime) / 1000;
    System.out.print("\rElapsed time: " + totalElapsed + "\nSites Done: " + siteNumber);
}

private String getResource(String someUrl) {
    String body = "";
    try {
        URI url = new URI(someUrl);
        HttpRequest request = HttpRequest.newBuilder()
                .uri(url).GET().build();
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandler.asString());
        body = response.body();
    } catch (URISyntaxException e) {
        System.out.println("URL " + someUrl + "is not valid");
    } catch (IOException | InterruptedException e) {
        System.out.println(e.getMessage());
    }
    return body;
}

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

    String[] topIranianSites = {
            "https://www.aparat.com/",
            "http://www.varzesh3.com/",
            "http://namnak.com/",
            "http://www.telewebion.com/",
            "https://divar.ir/",
            "https://www.ninisite.com/",
            "https://www.blogfa.com/",
            "http://www.namasha.com/",
            "http://www.yjc.ir/"
    };

    new ReqThreaded(topIranianSites);

}
}

即:

  • 切勿在不同线程上同时对同一对象运行相同的方法

  • 永远不要通过普通变量在线程之间交换信息。仅使用专门的设施。这里我使用CountDownLatch,表示每个线程的结束。如果我想向主线程返回一些信息,我会改用BlockingQueue

【讨论】:

  • 感谢改进代码,但多线程代码在哪里?我是java的新手,但也有同样的想法。我认为我的多线程代码实现中的主要问题是当多个线程试图访问对象的相同方法时。它有某种阻挡效果,我想你能详细说明一下吗?
  • 当我在第五个站点之后运行您的代码时,会出现从 1.8 秒到超过 3 秒的跳跃,并且在超过 7 秒后结束,这是为什么呢??
  • 您不能说“在第五个站点之后”,因为所有站点都是并行请求的:请求同时开始,但在站点回复时结束。确实,有些网站回复很快,有些则延迟。将总经过时间与单个站点的最长时间进行比较。如果它们很接近,那么没关系。
  • 谢谢亚历克斯。在您的提示和指导下,我编写了另一个多线程代码实现here,它在我的测试中运行得如此之快。跳闸时间范围在 0.2 - 1.3 秒之间,但在您的实施中,跳闸时间在 0.95 - 11.57 秒之间。既然您是并发专家,请您看一下那里的代码并告诉我为什么行程代码有如此大的差异?我想不通。
  • 我测试的网站是alexa的全球顶级列表
猜你喜欢
  • 1970-01-01
  • 2017-08-17
  • 2017-09-23
  • 2018-10-12
  • 2013-07-12
  • 2018-02-07
  • 1970-01-01
  • 2018-09-21
  • 2015-09-19
相关资源
最近更新 更多