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 / May 2007

Tip: Looking for answers? Try searching our database.

Interrupt a sleeping thread without interrupting I/O

Thread view: 
Chris - 09 May 2007 23:10 GMT
How do I interrupt a sleeping thread without interrupting any I/O that
it may be doing?

We've got a thread that reads data using an NIO channel, goes to sleep
for a minute, wakes up and reads some more, goes to sleep, etc.

I'd like to terminate the thread quickly, but gracefully. If it's
sleeping, I'd like to wake it up and have it exit; if it's reading data,
I want it to finish and then terminate.

The trouble is that if I call Thread.interrupt(), it will properly
interrupt a Thread.sleep(), but it will also interrupt a
FileChannel.read() operation with a ClosedByInterruptException. Ideally
I'd have a way to do only the former and not the latter.

Any ideas?
Nico - 09 May 2007 23:17 GMT
> How do I interrupt a sleeping thread without interrupting any I/O that
> it may be doing?
[quoted text clipped - 12 lines]
>
> Any ideas?

According to the javadoc, yes, the I/O will be interrupted.
You could set a boolean somewhere in a static field to know if the
thread is interruptible or not (doing IO or sleeping).
Tom Hawtin - 09 May 2007 23:19 GMT
> The trouble is that if I call Thread.interrupt(), it will properly
> interrupt a Thread.sleep(), but it will also interrupt a
> FileChannel.read() operation with a ClosedByInterruptException. Ideally
> I'd have a way to do only the former and not the latter.

Use wait and notify instead of interrupt.

If the thread may be in select, call wakeup.

Tom Hawtin
Knute Johnson - 09 May 2007 23:20 GMT
> How do I interrupt a sleeping thread without interrupting any I/O that
> it may be doing?
[quoted text clipped - 12 lines]
>
> Any ideas?

Synchronize the read and the interrupt.

Signature

Knute Johnson
email s/nospam/knute/

Tom Hawtin - 09 May 2007 23:54 GMT
>> I'd like to terminate the thread quickly, but gracefully. If it's
>> sleeping, I'd like to wake it up and have it exit; if it's reading
>> data, I want it to finish and then terminate.

> Synchronize the read and the interrupt.

That still leaves a race condition. Consider the I/O thread is leaving
the sleep and is about to acquire the lock. Meanwhile the interrupting
thread acquires the lock and interrupts. End result is that the
interrupt occurs in the first I/O operation.

A refinement is to check and clear the interrupt status between locking
and I/O, but a standard wait-notify should suffice.

Tom Hawtin
Knute Johnson - 10 May 2007 00:33 GMT
>>> I'd like to terminate the thread quickly, but gracefully. If it's
>>> sleeping, I'd like to wake it up and have it exit; if it's reading
[quoted text clipped - 11 lines]
>
> Tom Hawtin

Tom:

How would you implement that with wait/notify?

Signature

Knute Johnson
email s/nospam/knute/

Tom Hawtin - 10 May 2007 00:43 GMT
>>>> I'd like to terminate the thread quickly, but gracefully. If it's
>>>> sleeping, I'd like to wake it up and have it exit; if it's reading
>>>> data, I want it to finish and then terminate.

> How would you implement that with wait/notify?

In the I/O thread:

    for (;;) {
        performIO();
        synchronized (lock) {
            if (exit) {
                return;
            }

            lock.wait(delay);

            if (exit) {
                return;
            }
        }
    }

In interrupting thread:

    public void stopIOThread() {
        synchronized (lock) {
            exit = true;
            lock.notifyAll();
        }
    }

Tom Hawtin
Chris - 10 May 2007 00:56 GMT
>>>>> I'd like to terminate the thread quickly, but gracefully. If it's
>>>>> sleeping, I'd like to wake it up and have it exit; if it's reading
[quoted text clipped - 29 lines]
>
> Tom Hawtin

I'm confused. Suppose I/O thread is currently waiting, sitting in the
synchronized{} block. The interrupting thread tries to call
stopIOThread(). Wouldn't it just hang, waiting to get into the
synchronized{} block? It would never get a chance to call notifyAll()
until I/O thread had timed out.
Tom Hawtin - 10 May 2007 01:06 GMT
> I'm confused. Suppose I/O thread is currently waiting, sitting in the
> synchronized{} block. The interrupting thread tries to call
> stopIOThread(). Wouldn't it just hang, waiting to get into the
> synchronized{} block? It would never get a chance to call notifyAll()
> until I/O thread had timed out.

No. Wouldn't be much point in calling notifyAll ever if that were the case.

From the API docs for Object.wait():

  "The thread releases ownership of this monitor and waits until
another thread notifies threads waiting on this object's monitor to wake
up either through a call to the notify method or the notifyAll method."

Tom Hawtin
Chris - 10 May 2007 01:18 GMT
>> I'm confused. Suppose I/O thread is currently waiting, sitting in the
>> synchronized{} block. The interrupting thread tries to call
[quoted text clipped - 9 lines]
> thread notifies threads waiting on this object's monitor to wake up
> either through a call to the notify method or the notifyAll method."

Aha! Thanks. I just tried it and it worked perfectly. This solves the
problem.
Knute Johnson - 12 May 2007 18:08 GMT
>>>>> I'd like to terminate the thread quickly, but gracefully. If it's
>>>>> sleeping, I'd like to wake it up and have it exit; if it's reading
[quoted text clipped - 29 lines]
>
> Tom Hawtin

Thanks Tom, sorry I was so slow responding.

Signature

Knute Johnson
email s/nospam/knute/



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



©2009 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.