我正在寫一個IP掃描儀應用程序和過程是需要長時間因此,我在使用GUI 的回現場服務執行,如:如何從Swing worker運行服務執行程序?
public static List<Future<String>> checkThisIP(String ipStart, String ipEnd) throws UnknownHostException {
final ExecutorService es = Executors.newFixedThreadPool(10);
final List<Future<String>> futures = new ArrayList<>();
String ipStringStart;
String ipStringEnd;
String targetIpString;
//my update
ipStringStart = ipStart.substring(ipStart.lastIndexOf(".") + 1, ipStart.length());
ipStringEnd = ipEnd.substring(ipEnd.lastIndexOf(".") + 1, ipEnd.length());
targetIpString = ipStart.substring(0, ipStart.lastIndexOf(".") + 1);
if (!ipStart.equals(ipEnd)) {
for (int i = Integer.parseInt(ipStringStart); i <= Integer.parseInt(ipStringEnd); i++) {
String currentIp = targetIpString + i;
futures.add(runPingScan(es, currentIp));
}
} else {
futures.add(runPingScan(es, ipStart));
}
es.shutdown();
return futures;
}
public static Future<String> runPingScan(final ExecutorService es, final String ip) {
return es.submit(new Callable<String>() {
@Override
public String call() {
String returnMe = "";
//custom ping class
Ping p = new Ping();
//send message
p.SendReply(ip);
//IsReachable returns ture or false
if(p.IsReachable()){
returnMe=ip;
}
return returnMe;
}
});
}
這是使用的JButton原laggy代碼預製行動:
// scan result is Future list returned from service executor
List<Future<String>> scanResult = p.checkThisIP(jFormattedTextField1.getText(), jFormattedTextField2.getText());
for (final Future<String> f : scanResult) {
try {
ip = f.get();
if (!ip.equals("")) {
arp ARP = new arp();
PortScan openPort = new PortScan();
IP ipClass = new IP();
mac = ARP.getMac(ip);
manufacturer = ARP.getOUI(mac);
ports = openPort.checkIpForPorts(ip);
hostname = ipClass.hostname(ip);
title = ipClass.htmlTitle(ip);
Object[] data = {ip, mac, manufacturer, ports, hostname, title};
tableModel.addRow(data);
}
if (jFormattedTextField1.getText().equals(jFormattedTextField2.getText()) && ip.equals("")) {
JOptionPane.showMessageDialog(null, "<html>Can not ping the address ! <br> Server might be protected by <b>WAF</b>.</html>", "Alert", HEIGHT);
}
} catch (Exception ex) {
Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
}
}
運行這段代碼是好的,但是當我將其連接到開始掃描按鈕的GUI滯後,我一派,想通了使用搖擺工人。當我單獨實現了swing工作者的時候,它並沒有實現併發性,而且當我實現了gui仍然滯後時。無論如何,我的問題是讓按鈕(Swing worker)調用服務執行程序來執行其他進程嗎?
感謝您的解釋+1。請注意,Swingworker的第一個參數應該是List>,我試過了你的答案,但我面臨同樣的滯後。看看我的回答,我只使用了swingworker函數** doInBackground()**來調用使用** service executor **(Lag cause)的函數,而沒有其他的swingfunction函數。 –
第一個類型參數定義由'doInBackground'創建的數據,稍後通過調用'get'獲取數據。它只是一個字符串列表,不需要未來,因爲所有的ping檢查都已經完成。如果你在'doInBackground'內部做了所有的事情,確保你的模型正確地處理了gui更新(即'invokeLater'),因爲Swing不是線程安全的,任何接觸它的組件都應該從EDT開始。 – duckstep
據我所知,字符串列表不能取List>。 'List > scanResult = p.checkThisIP(ip1,ip2); //該函數返回Future列表''所以如果我們想從'doInbackground()'返回該列表,那麼第一個參數應該是'>'。 P.S:我試過自己試過了。謝謝 ! –