Added uptime parallelizing.

This commit is contained in:
Christian P. MOMON 2021-07-03 10:11:57 +02:00
parent 5b1ce71dd8
commit 893b54e013

View file

@ -21,10 +21,15 @@ package fr.devinsy.statoolinfos.uptime;
import java.io.IOException; import java.io.IOException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; 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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.statoolinfos.util.Chrono;
/** /**
* The Class UptimeChecker. * The Class UptimeChecker.
*/ */
@ -134,14 +139,50 @@ public class UptimeSurveyor
result.add(new Uptime(url)); result.add(new Uptime(url));
} }
Chrono chrono = new Chrono().start();
// //
System.out.println("Survey of " + result.size()); System.out.println("Survey of " + result.size());
/* MonoThread version.
for (Uptime uptime : result) for (Uptime uptime : result)
{ {
int code = survey(uptime.getURL()); int code = survey(uptime.getURL());
uptime.update(code); 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; return result;