From 893b54e013bb9bf2efa4f29e2ce3265e6296ab80 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Sat, 3 Jul 2021 10:11:57 +0200 Subject: [PATCH] Added uptime parallelizing. --- .../statoolinfos/uptime/UptimeSurveyor.java | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/fr/devinsy/statoolinfos/uptime/UptimeSurveyor.java b/src/fr/devinsy/statoolinfos/uptime/UptimeSurveyor.java index f0179a3..2d5d03a 100644 --- a/src/fr/devinsy/statoolinfos/uptime/UptimeSurveyor.java +++ b/src/fr/devinsy/statoolinfos/uptime/UptimeSurveyor.java @@ -21,10 +21,15 @@ package fr.devinsy.statoolinfos.uptime; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import fr.devinsy.statoolinfos.util.Chrono; + /** * The Class UptimeChecker. */ @@ -134,14 +139,50 @@ public class UptimeSurveyor result.add(new Uptime(url)); } + Chrono chrono = new Chrono().start(); + // System.out.println("Survey of " + result.size()); + /* MonoThread version. for (Uptime uptime : result) { int code = survey(uptime.getURL()); uptime.update(code); - System.out.println("Uptime: " + uptime.getStatus().toString() + " " + uptime.getURL().toString()); + System.out.println("Uptime: " + uptime.getStatus().toString() + " " + + uptime.getURL().toString()); } + */ + + ExecutorService pool = Executors.newFixedThreadPool(20); + for (Uptime uptime : result) + { + pool.execute(new Runnable() + { + @Override + public void run() + { + int code = survey(uptime.getURL()); + uptime.update(code); + System.out.println(Thread.currentThread().getId() + " Uptime: " + uptime.getStatus().toString() + " " + uptime.getURL().toString()); + } + }); + } + + pool.shutdown(); + while (!pool.isTerminated()) + { + try + { + System.out.println("Wait for URL survey."); + pool.awaitTermination(5, TimeUnit.SECONDS); + } + catch (InterruptedException exception) + { + exception.printStackTrace(); + } + } + + System.out.println("URL Survey time: " + chrono.toString()); // return result;