bloking operation in a thread  
Author Message
V関





PostPosted: 2004-7-4 3:00:00 Top

java-programmer, bloking operation in a thread Hi !
I have a poblem with threads : I program multi-threaded applications for a
short time.
I would know how it would be possible to stop a thread which is running a
bloking operation (such i/o functions, server accept(), ...). My app is
waiting indefinitely for the thread to die and that's normal. so how to
stop this thread ?


 
Robert Mark Bram





PostPosted: 2004-7-3 22:33:00 Top

java-programmer >> bloking operation in a thread Hi V関?

> I have a poblem with threads : I program multi-threaded applications for a
> short time.
> I would know how it would be possible to stop a thread which is running a
> bloking operation (such i/o functions, server accept(), ...). My app is
> waiting indefinitely for the thread to die and that's normal. so how to
> stop this thread ?

You can interrupt the thread - by calling interrupt() on it. This has some
consequences:
- your thread's blocking operation should be surrounded by a try-catch that
will deal with InterruptedException.
- the code that is responsible for interrpting the thread needs to have
reference to the thread itself in order to call interrupt on it.
- before interrupting the thread, you should set some sort of flag on your
thread to indicate it should be 'stopped' so that your io thread doesn't go
back to blocking..

Good luck!

Rob
:)


 
Roedy Green





PostPosted: 2004-7-3 23:13:00 Top

java-programmer >> bloking operation in a thread On Sat, 03 Jul 2004 14:59:57 -0400, V関?<email***@***.com> wrote
or quoted :

>I would know how it would be possible to stop a thread which is running a
>bloking operation (such i/o functions, server accept(), ...). My app is
>waiting indefinitely for the thread to die and that's normal. so how to
>stop this thread ?

see StoppableThread in the download at
http://mindprod.com/products.html#BUS

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
 
Joseph Dionne





PostPosted: 2004-7-4 5:28:00 Top

java-programmer >> bloking operation in a thread V関?wrote:
> Hi !
> I have a poblem with threads : I program multi-threaded applications for a
> short time.
> I would know how it would be possible to stop a thread which is running a
> bloking operation (such i/o functions, server accept(), ...). My app is
> waiting indefinitely for the thread to die and that's normal. so how to
> stop this thread ?
>
>

Use a boolean variable such as stopService, create a setter function and
have the parent set stopService true. Test stopService directly after
the accept(), then have the parent connect to your own service, thus
allowing the accept() to return.


Accept thread example;

... <some code, with a loop>

service.accept()
if (stopService) break

... <more code, with end of loop>

close the service, and exit.


Parent thread example;

myservice.setStop(true);
<connect to your own service ip and port>


I assume you are on Windows, which does not allow an accept() to be
interrupted.

Joseph