由於某種原因,即使在關閉應用程序的窗口並退出之後,我的應用程序中仍存在一些進程。有沒有我沒有考慮過的東西?我的應用程序會定期生成系統托盤通知,但這些通知即使在退出後也會繼續。停止它的唯一方法是在任務管理器中找到java.exe進程。有我忽略的東西嗎?爲什麼JavaFX應用程序任務管理器進程在關閉時沒有結束
public class TrayNotification {
public static void execute(String firstName, String incidentNumber, String requestId, TableView tabTable) throws AWTException, MalformedURLException {
if (SystemTray.isSupported()) {
TrayNotification trayNotification = new TrayNotification();
trayNotification.displayTray(firstName, incidentNumber, requestId, tabTable);
}
}
private void displayTray(String firstName, String incidentNumber, String requestId, TableView tabTable) throws AWTException, MalformedURLException {
// Obtain a single instance of SystemTray
SystemTray tray = SystemTray.getSystemTray();
// Set TrayIcon values
Image image = Toolkit.getDefaultToolkit().createImage(getClass().getResource("/images/tray-icon.png"));
final TrayIcon trayIcon = new TrayIcon(image, "Remedy React Notification");
trayIcon.setImageAutoSize(true);
trayIcon.setToolTip(incidentNumber);
// Add TrayIcon to SystemTray instance if possible
try {
tray.add(trayIcon);
} catch (AWTException e) {
// System.out.println("Notification could not be added.");
}
trayIcon.addActionListener(e -> {
Parent root = null;
try {
root = FXMLLoader.load(getClass().getResource("/fxml/scene.fxml"));
root.requestLayout();
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("Test");
});
// Display TrayIcon
trayIcon.displayMessage("Hey, " + firstName, "You are now tracking " + incidentNumber + " !", TrayIcon.MessageType.INFO);
}
}
最可能的原因是創建非守護線程。你是在程序中創建任何新線程,無論是明確的還是通過執行者?考慮嘗試提供[MCVE]。 – Itai
更多[here](https://stackoverflow.com/q/2213340/230513)和[here](http://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html #JLS-12.8)。 – trashgod