本文介绍了Java 9多线程HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道如何正确地将以下顺序代码转换为多线程.我已尽力避免任何共享资源和完全线程独立性.
I can't figure out how to covert the following sequential code to a multi-threaded properly. I have done my best to avoid any shared resource and complete thread independence.
这是单线程代码 包com.net;
This is the single-threaded code 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 ReqSingle {
private float totalElapsedTime = 0F;
private HttpClient client = HttpClient.newHttpClient();
public ReqSingle(String[] sties){
for (String site : sties){
long fetchStartTime = System.currentTimeMillis();
String html = 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: " + sties.length);
}
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) {
// do nothing
}
return body;
}
}
这是我的多线程代码的实现:
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 {
public ReqThreaded(String[] sites) {
for (String rawUri : sites) {
new Thread(new Site(rawUri)).start();
}
}
class Site implements Runnable {
URI uri;
public Site(String rawUri) {
try {
uri = new URI(rawUri);
} catch (URISyntaxException e) {
System.out.println("URL " + rawUri + "is not valid");
}
}
@Override
public void run() {
String html = "";
long fetchStartTime = System.currentTimeMillis();
try {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(uri).GET().build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandler.asString());
html = response.body();
} catch (IOException | InterruptedException e) {
//do nothing
}
float elapsed = (float) (System.currentTimeMillis() - fetchStartTime) / 1000;
Document doc = Jsoup.parse(html);
System.out.println("It took " + elapsed + " seconds to fetch " + uri.toString() + " with title " + doc.title());
}
}
}
这是测试器类,它的输出是:
And this is the tester class and it's output:
package com.net;
public class ReqTester {
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/"
};
System.out.println("Single Threaded Test Results:\n");
new ReqSingle(topIranianSites);
System.out.println("\n\nMulti Threaded Test Results:\n");
new ReqThreaded(topIranianSites);
}
}
输出:
WARNING: Using incubator modules: jdk.incubator.httpclient
Single Threaded Test Results:
It took 2.843 seconds to fetch https://www.aparat.com/ with title آپارات - سرویس اشتراک ویدیو
It took 0.459 seconds to fetch http://www.varzesh3.com/ with title
It took 0.52 seconds to fetch http://namnak.com/ with title نمناک
It took 2.231 seconds to fetch http://www.telewebion.com/ with title تلوبیون | مرجع پخش زنده و دانلود فیلم ، سریال و سایر برنامه های تلویزیون
It took 0.243 seconds to fetch https://divar.ir/ with title
It took 1.749 seconds to fetch https://www.ninisite.com/ with title نی نی سایت | راهنمای بارداری و بچه داری
It took 0.407 seconds to fetch https://www.blogfa.com/ with title
It took 1.796 seconds to fetch http://www.namasha.com/ with title نماشا - سرویس رایگان اشتراک ویدیو
It took 0.232 seconds to fetch http://www.yjc.ir/ with title
Total Elapsed Time: 10.48
Total Number of sites: 9
Multi Threaded Test Results:
It took 0.114 seconds to fetch https://divar.ir/ with title
It took 0.236 seconds to fetch http://www.yjc.ir/ with title
It took 1.519 seconds to fetch http://www.varzesh3.com/ with title
It took 1.587 seconds to fetch https://www.blogfa.com/ with title
It took 3.524 seconds to fetch http://namnak.com/ with title نمناک
It took 4.152 seconds to fetch https://www.ninisite.com/ with title نی نی سایت | راهنمای بارداری و بچه داری
It took 4.486 seconds to fetch http://www.namasha.com/ with title نماشا - سرویس رایگان اشتراک ویدیو
It took 4.725 seconds to fetch https://www.aparat.com/ with title آپارات - سرویس اشتراک ویدیو
It took 5.82 seconds to fetch http://www.telewebion.com/ with title تلوبیون | مرجع پخش زنده و دانلود فیلم ، سریال و سایر برنامه های تلویزیون
推荐答案
您的代码正确.但是建议通过使用一些执行程序服务实施.
Your code is correct. But it is recommended to control the number of threads by using some executor service implementation.
例如:
ExecutorService executor = Executors.newFixedThreadPool(poolSize);
executor.submit(new Site(rawUri));
即使有大量的rawuris.它将保持线程数不变.
Even with large number of rawuris. It will keep a tap on the number of threads.
这篇关于Java 9多线程HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!