long threads

I

Ike

Suppose I spawn a thread, to upload a file. This file may take a few minutes
to upload on the client connection, and I dont want my Swing GUI to hang
while it is uploading.

However, before the file has completed uploading, say, the user exits the
application. What happens to the thread I spawned? What must I do? How?

Thanks, Ike
 
G

Gordon Beaton

Suppose I spawn a thread, to upload a file. This file may take a few
minutes to upload on the client connection, and I dont want my Swing
GUI to hang while it is uploading.

However, before the file has completed uploading, say, the user
exits the application. What happens to the thread I spawned?

Why not just try it and see?

If System.exit() is called, all threads associated with the process
will terminate. The file transfer will not complete, which seems
reasonable enough.
What must I do? How?

You don't have to do anything. What do you *want* to do?

You might ask the user to confirm before exiting, because it would
abort the upload. But an application that doesn't exit when the user
tells it to is broken.

/gordon
 
F

Frank

Why not just try it and see?

If System.exit() is called, all threads associated with the process
will terminate. The file transfer will not complete, which seems
reasonable enough.


You don't have to do anything. What do you *want* to do?

You might ask the user to confirm before exiting, because it would
abort the upload. But an application that doesn't exit when the user
tells it to is broken.

A couple UI thoughts to keep things predictable for you and your user:
1) have a cancel button to cancel an active transfer
2) prompt on application closing about canceling an active transfer
3) have a check box to close on completion (hate to borrow an idea from MS
IE, but for the sake of simplicity and predictabilty...)

Relying on System.exit WILL kill the active thread, but doesn't give it a
chance to invalidate what it was able to transfer appropriately.

HTH

Frank
 
P

Phil...

Just wondering what happens to the file.
I suspect the OS on your machine will close the file on your machine.
What about the file on the target machine? Will it save the partial
file? Will it even know that the file is partial? If you try to upload
it again later will there be trouble if part of it is already there?
 
G

Gordon Beaton

Just wondering what happens to the file. I suspect the OS on your
machine will close the file on your machine. What about the file on
the target machine? Will it save the partial file? Will it even know
that the file is partial? If you try to upload it again later will
there be trouble if part of it is already there?

Every server, upon detecting that a client has closed the connection,
should itself close the connection and dispose of any resources
associated with that client (i.e. it should close the open file too).
Servers that don't do these things tend not to be particularly robust.
It isn't the clients responsibility to "be nice" to the server. The
server should attempt to deal with the situation in a reasonable
manner.

What is best in the OP's situation depends on things like the contents
of the file, the method used to transfer it, the purpose of the
transfer and so on.

However I'll repeat my earlier comment that not exiting when asked to
by the user is a sign that the software is broken. Ask the user to
confirm if that's appropriate, but respect his choice.

/gordon
 
X

xarax

Roedy Green said:
You must wait for the other thread to terminate. Read up on
Thread.wait and join. See http://mindprod.com/jgloss/thread.html

System.exit() doesn't wait for non-daemon threads. They
are terminated with extreme prejudice.


When the user wants to exit, just bring up a modal
dialog that explains there is file transfer in
progress. Do you want to wait for completion (YES or NO)
then terminate the application? Do you want to cancel
the terminate request (CANCEL)?

YES NO CANCEL

If the user clicks YES, then the application waits
on the file transfer thread to finish by calling
join(). If the user clicks NO, then the application
first calls interrupt() for the file transfer thread,
then calls join() to wait for graceful exit. If the
user clicks CANCEL, then the application returns to
normal operation.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top