A funny thing is happening ... ExecutorCompletionService

I have an application written in java that should find all available hosts on the network.

I am using InetAddress.isReachable()for this with a timeout of 2000 milliseconds.

I look at the current IP address of the local machine and based on this I try to reach other IP addresses ending in 1 - 255, skipping the IP address of the local computers.

Everything works fine single-threaded, it just takes a lot of time, since most IP addresses are not available because they do not exist, so use a 2-second timeout.

To expedite the work (and try concurrency in action :: Brian Goetz), I tried to use Futureand Callableetc.

Everything went perfectly.

However, I thought using ExecutorCompletionServiceto give my users a more flexible application so that they can see the results as they appear using

Future<Reach> reachedFuture = completionService.take();

Running this on a single processor machine with the following configuration will only identify one of the four available nodes:

private static final int poolSize = 10;
private static final int maxPoolSize = 10;
private static final long keepAliveTime = 120;

private static final LinkedBlockingQueue<Runnable> queue
        = new LinkedBlockingQueue<Runnable>(20);

private static final ExecutorService executorService
        = new ThreadPoolExecutor(poolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS, queue);

private static final CompletionService<Reach> completionService
        = new ExecutorCompletionService<Reach>(executorService);

Changing it on this quad-core processor also did not allow to detect all available hosts:

private static final int poolSize
        = Math.max(2,Runtime.getRuntime().availableProcessors());

private static final int maxPoolSize
        = Math.max(2,Runtime.getRuntime().availableProcessors());

By changing the timeout InetAddress.isReachable()to 10 seconds, make the last configuration workable.

Also, changing the configuration as follows on a quad-core processor, it also worked with a 2 second timeout:

private static final int poolSize = 2;
private static final int maxPoolSize = 2;

I missed something very obvious, why is this happening?

What stops InetAddress.isReachable(2000)from detecting all available nodes in my network?

Why is it trying to make multiple calls InetAddress.isReachable()?

+3
1

, script Mac, - . LinkedBlockingQueue, , . , isReachable() ConnectException, . @user423199?

:

http://pastie.org/2460991

, ? IP , ICMP- . , , . Java JRE .

, .

+1

Source: https://habr.com/ru/post/1770713/


All Articles