1

How could I most efficiently go through a database of 12,000 IP addresses and ping open a socket with them all? Currently I am working like this:

  • Get all the IPs in the list
  • For every IP, open up a new thread that opens a socket with the server and receives a packet from it.
  • On the new thread, update the information based on weather the IP is online, and what it returns when we open the socket.

My current solution works, it just takes ages to get through the database once. I am trying to find a way to get through the database extremely fast; however, I don't know where to begin. Any help is appreciated, thanks!

EDIT: I am opening a socket and sending a TCP packet, then storing the information.

Jacob
  • 2,769
  • 3
  • 21
  • 29

1 Answers1

2

try to use something like netty (NIO) for this. You can just spread out your requests to all the IPs (for whatever reason) in one thread and wait for the returns in one other thread. Your current approach uses threads to manage the asynchronous answers of all the remote hosts, which seems to be ok and will work, but producting threads just to wait is not this optimal. better open lots of connections, sent packets, wait for results and let the OS manage the waiting.

Use multiple Threads only, if you want to perform concurrent computations which take unpredictable time. If you have such clearly defined tasks, like opening lots of sockets, sending data and waiting for responses, use NIO frameworks in java (btw. normally a single thread should be sufficient here).

cybye
  • 1,171
  • 6
  • 13