bloking operation in a thread

  • Thread starter Robert Mark Bram
  • Start date
R

Robert Mark Bram

Hi Vé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
:)
 
?

=?iso-8859-1?b?Vul26Q==?=

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 ?
 
J

Joseph Dionne

Vévé said:
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
 

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,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top