我无法弄清楚如何将以下顺序代码正确地转换为多线程。我已尽力避免任何共享资源和完全独立的线程。
这是单线程代码 包com.net;
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());
}
}
}
这是我对多线程代码的实现:
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 تلوبیون | مرجع پخش زنده و دانلود فیلم ، سریال و سایر برنامه های تلویزیون
输出:
file contents 1
:a some text
:b some other text
:a more text
:b more other text
:c final text
file contents 2
答案 0 :(得分:1)
您的代码是正确的。但建议使用一些here实现来控制线程数。
e.g:
ExecutorService executor = Executors.newFixedThreadPool(poolSize);
executor.submit(new Site(rawUri));
即使有大量的rawuris。它将继续点击线程数。
答案 1 :(得分:0)
在您的示例中,没有理由自己创建线程;你可以简单地利用HttpClient#sendAsync
。从其文档:
使用此客户端和给定的响应处理程序异步发送给定的请求。
此外,您应该使用JMH来正确地对Java程序进行基准测试。请参阅:How do I write a correct micro-benchmark in Java?