how to stop a thread when it is waitting a socket?

J

JTL.zheng

I am using SwingWorker to make a new thread in Swing GUI

(someting about SwingWorker
http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
)

I use this thraed to run a ServerSocket object
when using it's method accept() to wait a socket connect
this thread stop here for waiting

how can I do to stop this thread if I don't want to wait any more?
I want to click a button and stop this thread....
how can I do that?

Thank you very much in advance.
 
C

crazzybugger

JTL.zheng said:
I am using SwingWorker to make a new thread in Swing GUI

(someting about SwingWorker
http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
)

I use this thraed to run a ServerSocket object
when using it's method accept() to wait a socket connect
this thread stop here for waiting

how can I do to stop this thread if I don't want to wait any more?
I want to click a button and stop this thread....
how can I do that?

Thank you very much in advance.

i think you can the close() call......... this will stop your
ServerSocket object from listening..... The object will throw an
SocketException !!! so you can catch the exception outside!!!

for example,
ServerSocket server=new ServerSocket();
/* your code ........................*/

and when you want to stop the socket from listening
call this
server.close();

your code
try{
while(someCondition){
Socket socks=server.accept();
/**do something with your socket */
}
}catch(SocketException e){
/*reaches here after you make a server.close() call! */
}
 
J

JTL.zheng

Thank you very much
It work out now

but ..
if there any way to stop the SwingWorker Thread?
I mean next time I may not use ServerSocket but other time-consuming
operation which haven't a close() method to invoke.
how can I stop the whole thread?
 
J

JTL.zheng

I have another question:

in a thread:
----------------------
BufferedReader in = new BufferedReader(new InputStreamReader(socket.
getInputStream()));
String str;
while ( (str = in.readLine()) != null ){
/** codes **/
}
 
M

Matt Humphrey

JTL.zheng said:
Thank you very much
It work out now

but ..
if there any way to stop the SwingWorker Thread?
I mean next time I may not use ServerSocket but other time-consuming
operation which haven't a close() method to invoke.
how can I stop the whole thread?

There is no way in general to stop a thread safely. Thread processes must
be developed with a mechanism to stop them. A common technique is for the
thread to periodically test the Thread.interrupted () status, or to
otherwise monitor some flag. If the thread has no way stop itself there is
nothing you can do to force it to stop. (You can lookup online elsewhere
why Thread.stop () should not be used.)

Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
M

Matt Humphrey

JTL.zheng said:
I have another question:

in a thread:
----------------------
BufferedReader in = new BufferedReader(new InputStreamReader(socket.
getInputStream()));
String str;
while ( (str = in.readLine()) != null ){
/** codes **/
}
--------------

when there is not data input..
the program is blocked in "in.readLine()"
how can I stop this thread?

You have two choices--you can either close the stream or you can set a
timeout on the socket (setSoTimeout). (Interrupting the thread might work--I
havn't tried it and I don't recall reading about it working.) Setting the
socket timeout is good for detecting client (or server) failure, although
you have to combine it with something that makes a periodic request
(heartbeat.). For example, to detect client failure, the server sets the
socket timeout to 600 seconds (10 minutes). The client heartbeat makes a
request every 300 seconds that it *doesn't* already make a request. The
socket throws the timeout exception when the heartbeats are no longer
detected.

Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
J

JTL.zheng

Thank you very much for your help

I choose to close the stream
it's just a very simple point to point messager
so I didn's use the setSoTimeout
 

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
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top