Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
JTL.zheng - 09 Oct 2006 12:42 GMT
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.
crazzybugger - 09 Oct 2006 13:18 GMT
> I am using SwingWorker to make a new thread in Swing GUI
>
[quoted text clipped - 11 lines]
>
> 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! */
 }
JTL.zheng - 09 Oct 2006 14:01 GMT
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?
JTL.zheng - 09 Oct 2006 14:08 GMT
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?
Matt Humphrey - 09 Oct 2006 18:25 GMT
>I have another question:
>
[quoted text clipped - 11 lines]
> 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 matth@ivizNOSPAM.com http://www.iviz.com/
JTL.zheng - 10 Oct 2006 05:07 GMT
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
Matt Humphrey - 09 Oct 2006 14:09 GMT
> Thank you very much
> It work out now
[quoted text clipped - 4 lines]
> 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 matth@ivizNOSPAM.com http://www.iviz.com/
JTL.zheng - 09 Oct 2006 16:29 GMT
Thank you very much
I have learnt something about it now..


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.