multithreading - Ping multiple servers in Java -


this question has answer here:

i have program sends ping request servers. list large , if ip unreachable takes time go next ip.

i wish that, every ip, should create new thread & process of them simultaneously.

here code:

for (int = 0; < 89; i++) {      processbuilder processbuilder = new processbuilder("ping", iswindows? "-n" : "-c", "1", buttons[i].gettext());      process proc = processbuilder.start();      returnval = proc.waitfor();                        } 

how can make code ping ips, each in separate thread ?

how doing without processbuilder suggested others also.

i have 3 classes - pingparallel main class, pingtask task performed each thread, , pingresult having result code (we can add more info also, status message etc.).

pingparallel

package com.test.thread;  import java.util.arraylist; import java.util.date; import java.util.list; import java.util.concurrent.callable; import java.util.concurrent.executorservice; import java.util.concurrent.executors; import java.util.concurrent.future;  public class pingparallel {      public static void main(string[] args) {         int totalips = 89;         executorservice executor = executors.newfixedthreadpool(totalips);         list<future<pingresult>> list = new arraylist<future<pingresult>>();         callable<pingresult> callable = null;         for(int i=0; i< totalips; i++){             callable = new pingtask("127.0.0"+i); // ipaddres buttons[i].gettext());             future<pingresult> future = executor.submit(callable);             list.add(future);         }         for(future<pingresult> fut : list){             try {                 system.out.println(new date()+ "::"+fut.get());             } catch (exception e) {                 e.printstacktrace();             }         }         executor.shutdown();     } } 

pingtask

package com.test.thread;  import java.net.inetaddress; import java.util.concurrent.callable;  public class pingtask implements callable<pingresult> {    private string ipaddress;    public pingtask(string ipaddress) {     this.ipaddress = ipaddress;   }    @override   public pingresult call() {     inetaddress inet = null;     try {       inet = inetaddress.getbyname(ipaddress);       int resultcode = inet.isreachable(5000) ? 0 : -1;       return new pingresult(ipaddress, resultcode);     } catch (exception e) {       e.printstacktrace();       return new pingresult(ipaddress, -1);     }   } } 

pingresult

package com.test.thread;  public class pingresult {    private string ipaddress;   private int resultcode;    public pingresult(string ipaddress, int resultcode) {     this.ipaddress = ipaddress;     this.resultcode = resultcode;   }    public string getipaddress() {     return ipaddress;   }    public int getresultcode() {     return resultcode;   }    public string tostring() {     return "ipaddress :: "+ ipaddress + " result code : "+ resultcode;   } } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -