Make sure all process are run before exit?

R

Ryan Stewart

I have a class that needs to do some shutdown processing when the user
exits. However, if I use the following code, it seems to end program
execution before the shutDown() method is complete. (OuterClass is a
JFrame.)

private class WinCloser extends WindowAdapter {

public void windowClosing (WindowEvent we) {
OuterClass.this.shutDown();
System.exit(0);
}

}

One way I've found to deal with it is to do the following:

private class WinCloser extends WindowAdapter {

public void windowClosing (WindowEvent we) {
OuterClass.this.setVisible(false);
OuterClass.this.shutDown();
try{Thread.sleep(1000);}catch(Exception e){}
System.exit(0);
}

}

Like that the window goes away so it appears closed to the user, and the
program has an additional second to finish its shutdown procedures. This
seems sloppy to me, though. Is there a better way to do this?
 
E

E.C. Bäck

Ryan Stewart said:
I have a class that needs to do some shutdown processing when the user
exits. However, if I use the following code, it seems to end program
execution before the shutDown() method is complete. (OuterClass is a
JFrame.)

private class WinCloser extends WindowAdapter {

public void windowClosing (WindowEvent we) {
OuterClass.this.shutDown();
System.exit(0);
}

}

One way I've found to deal with it is to do the following:

private class WinCloser extends WindowAdapter {

public void windowClosing (WindowEvent we) {
OuterClass.this.setVisible(false);
OuterClass.this.shutDown();
try{Thread.sleep(1000);}catch(Exception e){}
System.exit(0);
}

}

Like that the window goes away so it appears closed to the user, and the
program has an additional second to finish its shutdown procedures. This
seems sloppy to me, though. Is there a better way to do this?

I don't know what the model for this, but my guess is that the window is
running in it's own gui thread launched from your app, and that
System.exit(0) forces the whole app--and the gui thread to exit. Perhaps
you want to wait until the gui thread notifies its termination?
--
Thanks,
Elliott C. Bäck

Sophomore, Computer Science
Cornell University
 
R

Ryan Stewart

E.C. Bäck said:
I don't know what the model for this, but my guess is that the window is
running in it's own gui thread launched from your app, and that
System.exit(0) forces the whole app--and the gui thread to exit. Perhaps
you want to wait until the gui thread notifies its termination?
--
Thanks,
Elliott C. Bäck

Sophomore, Computer Science
Cornell University

Yeah, it's something like that. I forgot about wait/notify. I'm just
learning Java on my own and haven't used that yet. I'll have to read up on
it.
 
X

xarax

Ryan Stewart said:
I have a class that needs to do some shutdown processing when the user
exits. However, if I use the following code, it seems to end program
execution before the shutDown() method is complete. (OuterClass is a
JFrame.)

private class WinCloser extends WindowAdapter {

public void windowClosing (WindowEvent we) {
OuterClass.this.shutDown();
System.exit(0);
}

}

One way I've found to deal with it is to do the following:

private class WinCloser extends WindowAdapter {

public void windowClosing (WindowEvent we) {
OuterClass.this.setVisible(false);
OuterClass.this.shutDown();
try{Thread.sleep(1000);}catch(Exception e){}
System.exit(0);
}

}

Like that the window goes away so it appears closed to the user, and the
program has an additional second to finish its shutdown procedures. This
seems sloppy to me, though. Is there a better way to do this?

Your example implies that the shutDown() method is returning
before shutdown processing is complete. The shutDown() method
must finish its processing before returning to its caller. If
shutDown() is communicating with another thread, then it must
use wait()/notify()/join() mechanism to wait until the other
thread signals completion.

An alternative is to move the call to System.exit(0) to the
other thread and have shutDown() simply post a message to
the other thread to initiate shutdown processing. The your
listener that is running the AWT Event Dispatch Thread can
simply return to its caller. When the other thread completes
its processing, it will call System.exit(0), rather than the
AWT EDT.

Yet another alternative is to use the JVM Shutdown Hook
mechanism where you can register a Runnable object that
will get invoked in its own thread after System.exit(0)
is called. You can call System.exit(0) from any thread
and your Runnable object will get control later in another
thread as the JVM is performing its own shutdown processing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top