java awt 监视器_java – 停止所有Awt / Swing线程和监视器和东...

看看AWT Threading Issues,它解释了退出AWT应用程序的标准.您要关注的部分如下:

Therefore, a stand-alone AWT application that wishes to exit cleanly

without calling System.exit must:

Make sure that all AWT or Swing components are made undisplayable when the application finishes. This can be done by calling 07001 on all top-level Windows. See 07002.

Make sure that no method of AWT event listeners registered by the application with any AWT or Swing component can run into an infinite loop or hang indefinitely. For example, an AWT listener method triggered by some AWT event can post a new AWT event of the same type to the EventQueue. The argument is that methods of AWT event listeners are typically executed on helper threads.

一个快速示例应用程序来演示……

import java.awt.Frame;

import javax.swing.JOptionPane;

import javax.swing.SwingUtilities;

public class CloseAWT

{

private static boolean running = true;

private static int response = -1;

public static void main(String[] args)

{

boolean showSwing = true;

boolean checkFrames = true;

while (running)

{

if (showSwing)

{

SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

response = JOptionPane.showConfirmDialog(null, "Hello World?");

}

});

showSwing = false;

}

else

{

if (response >= 0 && checkFrames)

{

SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

// topFrame.dispose();

Frame[] frames = Frame.getFrames();

System.out.printf("frames.length=%d

", frames.length);

}

});

checkFrames = false;

}

}

}

}

}

为了确认行为符合预期,我在JProfiler中运行了这个.单击“是”以关闭确认对话框后,’AWT-EventQueue-0’线程被标记为已死.在此之后唯一活着的线程是’main’和侦听Ctrl-Break的线程.

我强烈建议使用像JProfiler,YourKit,JProbe或其中一个免费的分析器,以确保您已正确释放所有组件并删除所有听众.

最后一个想法……您可能想要考虑将GUI作为一个单独的进程生成,并使用某种IPC在您的守护进程和GUI之间传递信息.虽然这会产生额外进程和IPC的额外开销,但它可以让您更好地确保在不再需要GUI时完全清理它.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值