Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A general way to do these kinds of tasks is, first, create a class to hold the results you want to get from each thread:</p> <pre><code>final class PingResult { public String ip; public String hostname; //... other things you want go here } </code></pre> <p>Then create a callable that does the actual work</p> <pre><code>class PingTask extends Callable&lt;PingResult&gt;{ private final String gateWayKey, subNetKey; //... other parameters you need go here public Ping( String gwKey, String snKey /*+ others? */ ){ // This is just a way to pass parameters to your pinging function this.gateWayKey = gwKey; this.subNetKey = snKey; // ... } public PingResult call(){ // Do actual pinging work here if ( /* Success */ ) { PingResult result = new PingResult(); result.ip= /*Fill these in*/; result.hostname = /* ... */; return result; } // Otherwise we failed, I'm using null as a failure sentinel // (you can come up with something better) return null; } } </code></pre> <p>Then in your calling code, set up a thread pool, cue up the requests, and then process the results.</p> <pre><code>// Might need to tweak the # for best performance final int NUM_THREADS = Runtime.getRuntime.availableProcesses(); ExecutorService exec = Executors.newFixedThreadPool( NUM_THREADS ); List&lt;Future&lt;PingResult&gt;&gt; results = new ArrayList&lt;PingResult&gt;(); for(/* ... */){ results.add( exec.submit( new PingTask( gateway, subnet ) ) ); } for( Future&lt;PingResult&gt; future : results ){ PingResult result = future.get(); // Process the result here (this is where you insert into the DB) } exec.shutdown(); // VERY IMPORTANT. If you don't do this the JVM will never stop. </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload