I am creating a monitor application that can monitor 100-150 devices...Now to design a monitor application, I have two approaches:-
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).
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?
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...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With