|
Description
|
import java.awt.*;
import java.awt.event.*;
class MyFrame extends Frame implements WindowListener
{
MyFrame(String name)
{
super(name);
setBounds(100, 100, 100, 100);
setVisible(true);
addWindowListener(this);
}
public void windowClosed(WindowEvent event) {
}
public void windowDeiconified(WindowEvent event) {
}
public void windowIconified(WindowEvent event) {
}
public void windowActivated(WindowEvent event) {
}
public void windowDeactivated(WindowEvent event) {
}
public void windowOpened(WindowEvent event) {
}
public void windowClosing(WindowEvent event)
{
dispose();
}
}
public class AWTTest
{
public static void main(String[] args)
{
Object queue = new Object();
MyFrame fr = new MyFrame("Window");
}
}
This program never exits because AWT threads are still alive.
Why these threads are not daemon threads ?
I think it's a bug. It would be a cleaner way to exit AWT than
calling System.exit().
======================================================================
|
|
Comments
|
Submitted On 31-MAR-1998
ScottEllsworth
4038690 closed?<p>
Calling system.exit() is NOT the same as allowing it to die
gracefully.<p>
There are two major reasons why this bug should not be closed, and why
"Call system.exit()" is not a sufficient workaround.<p>
1. Every app of reasonable size needs a thread to track every window, and when
the last window is closed, it needs to call system.exit. This is an
unnaceptable burden on a general application, as it may have a large number of
unrelated windows, and the top level objects should not have to know about low
level details, like whether a dialog box is open. The AWT event thread already
has to know which windows are alive, and so it would not be that hard to put in
some event switch code.<p>
2. On a popular VM, it has the capability of running multiple applets or
applications, all of which are shut down when system.exit is called. Given
that they work together in peace if the awt is not used, this is an unusual
contraint.<p>
The workaround:<br>
When the awt thread is created initially, create it as a daemon. When any
object capable of receiving awt events is added, set the thread to non daemon.
As soon as the awt event thread has nobody to communicate with (because all the
listeners are gone), switch it to daemon.<p>
This will keep all old programs working, but will make it possible to have more
sensible threading.<p>
Scott
PLEASE NOTE: JDK6 is formerly known as Project Mustang
|