How to stop a Thread, which reads indefinitly from a stream

Stopping a thread always gives me some headache. This time i have a class written which usesa Thread to indefinitly reads data from a stream. Now it shall be possible for the environment, which instantiated this communication class to stop the work.
The big problem is that the stream which is read isnt a fluent one, moreover a slow consumer/producer thing over com-port. So if i try somethign like this:


run()
{
     while(exec)
    {
            myreader.read() // blocking method call!
     }
}

stop()
{
        exec = false;
}

it can take quite a time that the flag is noticed to be negative so that the loop exists. how can i elegantly stop and dispatch the Thread?

Thread.interrupt() causes most blocking IO to throw an exception, which can be caught & used to exit the thread.

What about a non-blocking read? Or reading the number of available bytes first?
This is the official Thread guide by Sun:
http://java.sun.com/j2se/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html

I would strongly suggest you follow the guide by Sun, that way you won’t run in any
problems (interrupt() might work, but not very well in all situations)

That’s the way I (and Sun!) do it:


<class variable>
   private Thread blinker;
<code to start blinker>
   blinker = new Thread(this);
   blinker.start()
<to stop blinker do>
   blinker = null;

public void run() {
        Thread thisThread = Thread.currentThread();
        while (blinker == thisThread) {
            if (myreader.hasBytesAvailable()) {
                 myreader.readAllBytes();
            }

            try {
                thisThread.sleep(interval);
            } 
            catch (InterruptedException e){
              // never happens, so no need to handle that
            }
        }
    }

thats a goog dosurce to think about, thx for the hint