Shut down harvester thread pool when it's done. (#223)

* Shut down harvester thread pool when it's done.

* Use ExecutorService to create harvester threads; name thread pool threads to match other ice4j threads.
This commit is contained in:
Jonathan Lennox 2021-04-08 15:51:51 -04:00 committed by GitHub
parent 34c2ce50ec
commit 5a299cd694
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,6 +17,7 @@ package org.ice4j.ice.harvest;
import org.ice4j.*;
import org.jetbrains.annotations.*;
import org.jitsi.utils.concurrent.*;
import java.net.*;
import java.util.*;
@ -283,42 +284,49 @@ public class MappingCandidateHarvesters
// Now run discover() on all created harvesters in parallel and pick
// the ones which succeeded.
ExecutorService es = Executors.newFixedThreadPool(tasks.size());
ExecutorService es = ExecutorFactory.createFixedThreadPool(tasks.size(),
"ice4j.Harvester-executor-");
List<Future<StunMappingCandidateHarvester>> futures;
try
{
futures = es.invokeAll(tasks);
}
catch (InterruptedException ie)
{
Thread.currentThread().interrupt();
return stunHarvesters;
}
for (Future<StunMappingCandidateHarvester> future : futures)
{
List<Future<StunMappingCandidateHarvester>> futures;
try
{
StunMappingCandidateHarvester harvester = future.get();
// The STUN server replied successfully.
if (harvester.getMask() != null)
{
stunHarvesters.add(harvester);
}
futures = es.invokeAll(tasks);
}
catch (ExecutionException ee)
{
// The harvester failed for some reason, discard it.
}
catch(InterruptedException ie)
catch (InterruptedException ie)
{
Thread.currentThread().interrupt();
throw new RuntimeException(ie);
return stunHarvesters;
}
for (Future<StunMappingCandidateHarvester> future : futures)
{
try
{
StunMappingCandidateHarvester harvester = future.get();
// The STUN server replied successfully.
if (harvester.getMask() != null)
{
stunHarvesters.add(harvester);
}
}
catch (ExecutionException ee)
{
// The harvester failed for some reason, discard it.
}
catch (InterruptedException ie)
{
Thread.currentThread().interrupt();
throw new RuntimeException(ie);
}
}
}
finally
{
es.shutdown();
}
return stunHarvesters;
}