Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't stop ExecutorService in java
    primarykey
    data
    text
    <p>I have a problem with ExecutorService. I have a button in GUI that calls Runnable class that contains ExecutorService. I tried everything to stop ExecutorService (or main thread) but I didn't find a way to end it. Here is my code. Please post your answers and advises. Best regards.</p> <pre><code>public void actionPerformed(ActionEvent e) { final FindGateWaysAndIps scanner = new FindGateWaysAndIps(); if (e.getActionCommand()=="Start Scan"){ scanner.start(); } if (e.getActionCommand()=="Stop Scan"){ scanner.interrupt(); scanner.stopScans(); } } </code></pre> <p>Class "FindGateWaysAndIps"</p> <pre><code>String ip = "192.168."; String sql =" "; static volatile boolean stop = false; PingResult AllResaults = new PingResult(); int [] AllGateWays = new int [256]; final int NUM_THREADS = Runtime.getRuntime().availableProcessors(); ExecutorService exec = Executors.newFixedThreadPool(NUM_THREADS*5); public void run() { stop=true; while(stop){ for (;GateWayKey&lt;=GateWayKeyStop;GateWayKey++){ if (!stop){ exec.shutdownNow(); Thread.currentThread().interrupt(); break; } ip="192.168."+GateWayKey+".1"; AllSQLs.add(exec.submit((new PingTask(ip,GateWayKey,true)))); } if (!stop) { exec.shutdownNow(); Thread.currentThread().interrupt(); break; } AllGateWays=GetVectorData.GiveMeGateWays(); for (int j=0; j&lt;= AllGateWays.length;j++){ System.out.println("stop je: "+stop); if (!stop){ exec.shutdownNow(); Thread.currentThread().interrupt(); break; } removeDuplicateinVectors(); //System.out.println("Sada je j"+j); for (;SubNetKey&lt;=SubNetKeyStop;SubNetKey++){ if (!stop){ exec.shutdownNow(); Thread.currentThread().interrupt(); break; } ip="192.168."+AllGateWays[j]+"."+SubNetKey; AllSQLs.add (exec.submit((new PingTask(ip,AllGateWays[j],false)))); } // Process the result here (this is where you insert into the DB) //WriteAllDataIntoDataBase(); } exec.shutdown(); //WriteAllDataIntoDataBase(); } public void stopScans(){ exec.shutdownNow(); stop=false; } </code></pre> <p>Sorry here is PingTask class</p> <pre><code>public class PingTask implements Callable &lt;String&gt; { String ips; String sql; PingResult PassDataToExternalClass = new PingResult(); //FindGateWaysAndIps DataProccesor = new FindGateWaysAndIps(); int GateWay; ScanFrame MonitorData = new ScanFrame(); boolean GateWayORSubNet; int [] AllGateWays = new int [256]; int i=0; public int[] GiveMeGateWays(){ return AllGateWays; } public PingTask (){ } public PingTask (String ip, int GateWayKey, boolean GateWayORSubNets){ ips=ip; GateWay=GateWayKey; GateWayORSubNet=GateWayORSubNets; } public String call(){ InetAddress address; try { address = InetAddress.getByName(ips);//ako nade gateway neka skoci u petlju u kojoj nade IP adrese pripadajuceg gatewaya System.out.println("PINGAM: "+ips); try { if (address.isReachable(2000)) { //pinga gatewaya s 1000ms (jeli je moguce ovo smanjiti da se ubrza proces)? System.out.println("Nasa sam IP: "+ips); AllGateWays[i]=GateWay; i++; MonitorData.WriteMonitorData(ips,address.getHostName().toString(),"2000","da"); if (GateWayORSubNet){ sql="REPLACE INTO `gateways` (`ID_GATEWAY` , `GATEWAY_IP` , `GATEWAY_NAME`) VALUES ('"+GateWay+"', '"+ips+"', '"+address.getHostName().toString()+"');"; return sql; } else{ sql="REPLACE INTO `subnets` (`IP` , `COMPUTER_NAME` , `GATEWAY_KEY`) VALUES ('"+ips+"', '"+address.getHostName().toString()+"', '"+GateWay+"');"; return sql; } } else { return ";"; } } catch (IOException e) { return ";"; } } catch (UnknownHostException e) { return ";"; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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