15
votes

I am creating a monitor application that can monitor 100-150 devices...Now to design a monitor application, I have two approaches:-

  1. Create a thread for each of the device to monitor and each thread will ping (Using ICMP) to device to know whether the device is online or not. These threads will run indefinately to know their status after a particular time interval (say 60 seconds).

  2. Create a thread pool and for each device, submit a task to a thread pool. The task is simple ping to a device. So, in the current design, the tasks will be more than the threads in thread pool. For instance, say there are 100 device to monitor, there will be 100 tasks to be monitored and thread pool will have say 40 threads to complete these 100 tasks. Off course the time duration to run the next bunch of tasks will be such that to complete all the pending tasks in thread pool.

Which approach will be better?

4

4 Answers

12
votes

I'd create a ScheduledExecutorService (e.g. via Executors.newScheduledThreadPool) allowing you to schedule repeated pings. Do you know how long each ping is likely to take? I would hope that you could get away with very few threads - far fewer than 40, if you only need to ping each of 100 devices once per minute.

You can start it with a reasonably small number of threads - but be aware of the information Mark Peters raised in the comments - the implementation used does not expand (as I'd expected it to); it's effectively a fixed thread pool. Still, you should be able to do with fewer than 40...

2
votes

Creating a separate thread for each device is not a scalable solution. Just imagine if your solution has to work with a increased number of devices, say from 100 to 1000; you cannot keep spawning a separate every time.

Creating a threadpool would be an desired solution. You can tune the size of the threadpool as per the requirement and resource availability. If you need to ping the devices on recurrent basis after a fixed interval then you can go for a scheduled executor service which internally takes care of scheduling the tasks periodically. But yes, using a threadpool is the obivous choice.

2
votes

You should to take into account the ping timeout when calculating maximum number of threads. How many threads require pinging 100 devices, given a ping timeout of 5 seconds, and repeat period of 60 seconds?

In 60 seconds each thread could perform 12 device pings (60 seconds allow 12 pings to timeout after 5 seconds). That means you need at least 9 threads each performing 12 pings per 60 seconds.

numberOfThreads = numberOfDevices / (pollingPeriod/pingTimeout) = 100 / (60/5) = 100/12 = 9 threads

1
votes

I would suggest going for a thread pool instead of creating a new thread for every device. Thread pools are suited for a lot of tasks which are of short duration and your problem fits perfectly into this.