System.exit

A

Abraham Khalil

Before user is about to close the gui, I want to free up the open
sockets used in my application. Now, we don't want to poll the exit,
waiting for the open sockets to finally free themselves and than the
application quits. The user is going to think something is wrong with
the application and wonder why its taking to long to exit :)

So, I thought of putting the close sockets in a thread and start it,
but the next line after starting a new thread in the main thread code
is System.exit. Now once that calls, the free sockets thread won't do
any work on closing the sockets, because the JVM is realease from its
resource. Now I'am damm if don't use a another thread,
or damm if using a thread (As it doesn't do anything).

Can I have a Thread as another JVM to free the sockets while the
application exit?

Thanks
 
G

Gordon Beaton

Before user is about to close the gui, I want to free up the open
sockets used in my application. Now, we don't want to poll the exit,
waiting for the open sockets to finally free themselves and than the
application quits. The user is going to think something is wrong
with the application and wonder why its taking to long to exit :)

So, I thought of putting the close sockets in a thread and start it,
but the next line after starting a new thread in the main thread
code is System.exit. Now once that calls, the free sockets thread
won't do any work on closing the sockets, because the JVM is
realease from its resource. Now I'am damm if don't use a another
thread, or damm if using a thread (As it doesn't do anything).

Can I have a Thread as another JVM to free the sockets while the
application exit?

I don't see why you need to handle this in a separate thread at all.
It sounds like you just need a simple addition to your main method
after starting that thread you mentioned (and waiting for it to
finish):

public static void main(String[] args) {

//...

try {
Thread t = new Thread(...);
t.start();
t.join();
}
finally {
for (all the stuff that needs closing) {
thing.close();
}
}

System.exit(0);
}

Alternatively, you could use shutdown hooks (see
Runtime.addShutdownHook()).

Remember though that most operating systems will close your open
sockets when the process terminates anyway, and any server that can't
deal with clients abruptly closing is broken. Depending on what you're
actually using those sockets for (and assuming that all *critical*
data has been acknowledged) then it shouldn't matter one way or the
other.

/gordon
 
E

EJP

I don't know why you're worrying about the sockets at all. The OS will
close them when the process exits.

Gordon said:
Before user is about to close the gui, I want to free up the open
sockets used in my application. Now, we don't want to poll the exit,
waiting for the open sockets to finally free themselves and than the
application quits. The user is going to think something is wrong
with the application and wonder why its taking to long to exit :)

So, I thought of putting the close sockets in a thread and start it,
but the next line after starting a new thread in the main thread
code is System.exit. Now once that calls, the free sockets thread
won't do any work on closing the sockets, because the JVM is
realease from its resource. Now I'am damm if don't use a another
thread, or damm if using a thread (As it doesn't do anything).

Can I have a Thread as another JVM to free the sockets while the
application exit?

I don't see why you need to handle this in a separate thread at all.
It sounds like you just need a simple addition to your main method
after starting that thread you mentioned (and waiting for it to
finish):

public static void main(String[] args) {

//...

try {
Thread t = new Thread(...);
t.start();
t.join();
}
finally {
for (all the stuff that needs closing) {
thing.close();
}
}

System.exit(0);
}

Alternatively, you could use shutdown hooks (see
Runtime.addShutdownHook()).

Remember though that most operating systems will close your open
sockets when the process terminates anyway, and any server that can't
deal with clients abruptly closing is broken. Depending on what you're
actually using those sockets for (and assuming that all *critical*
data has been acknowledged) then it shouldn't matter one way or the
other.

/gordon
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top