I have a arraylist which consists of 5000 IP Addresses. For each IP Address, I want to execute a SNMPGet request and a FTPDownload command. I want to implement it in a fashion, wherein at a time 2 different threads run simultaneously for the first five IP Addresses. After the execution of these IP Addresses, next 2 IP Addresses will be executed on these threads. Can anyone help how to do it?
Here, connection is a class which extends the thread and the work to be implemented is written in its run() method. Please help.
Connection newConnection =new Connection(0);
Connection newConnection1 =new Connection(1);
for(int i = 0; i < NE_list.getRowCount(); i=i+2)
{
if(NE_list.getValueAt(i, 0).toString().equals("true")) //Some condition here for the IP Address
{
newConnection.i=i;
newConnection1.i=i+1;
newConnection.runprogram();
newConnection1.runprogram();
}
}
class Connection extends Thread{
int i;
Connection(int val){
i=val;
}
void runprogram(){
start();
}
public void run(){
//SNMP and FTP Code here for IP Address in index i of NE_list
}
}
Herewith adding working example having 5 threads. Just put the test.txt in your CLASS_PATH of the application.
Main Class -
Executor Framework will be best suit for your solution. I have created one example here. You can increase the number of threads as per your requirement.