【发布时间】:2018-01-24 08:02:30
【问题描述】:
由于某种原因,我的应用程序中的某些进程即使在关闭应用程序窗口并退出后仍会继续。有什么我没有考虑到的吗?我的应用程序会定期生成系统托盘通知,但这些通知即使在退出后也会继续。阻止它的唯一方法是在任务管理器中找到 java.exe 进程。有什么我忽略的吗?
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);
}
}
【问题讨论】:
-
最可能的原因是创建了非守护线程。您是否在程序中显式地或通过执行程序创建任何新线程?考虑尝试提供minimal reproducible example。