Mike Marchywka wrote:

> In the DayTime Server example I have, there appears to be a call
> to Thread.stop() ( on page 301 in Hunter/Crawford)to stop the
> daemon thread. Is this accepted procedure or is there a more recent
> edition?
> Do you guys have any comments on using Thread.stop()
> in general, not just in servlets? I do this, optionally, in
> applets when everything goes bad and the only apparent alternative
> is hanging a browser. How do you kill a thread stuck waiting to get
> into a synchronized block? Is this improved in 1.4?
>
> It doesn't look like there is really anything more dangerous about
> launching a thread
> from a servlet than from anything else(especially compared to an applet
> inside
> of an undetermined browser!).
>
> Thanks.
> [...]

Hello Mike, I found the following ways to "end" a loop-thread
from several books and emails,  I am also trying to use it in
init/destroy or ServletContextListener of  Servlet   :-)

Class MyClass implements Runnable
    Thread thisThread; //a reference to that endless-loop-Thread
    volatile boolean isAlive; ..a flag
    InputStream is(or socket) ; //Try(not sure) to end the io-loop by closing
is/socket

    public void run(){
       thisThread=Thread.currentThread();

       while(true){ //or while(isAlive)
          //way0
          if(Thread.currentThread().isInterrupted()) return/break;

          //way1
          if(!isAlive) return/break;

          //way2  to end sleep/join/wait by InterruptedException
           try{
                    Thread.sleep()
                    or
                    Thread.join()
                     or
                     synchronized-Object.wait()
                }catch(InterruptedException e)  {return/break;}


          //method3
           try{
                   for io:  I guess it is possible to end io by ending  the
underlying
                   socket/stream, then catch the NullPointerException or other
                   Error/Exception.
          }catch(...){return/break;}
      }
  }

   //invoked by other Thread
   public void synchronized end(){
         if(is!=null) {is.close(); is=null;}
         if(thisThread!=null && thisThread.isAlive()) {
thisThread.interrupt();}
         isAlive=false; //end() method is a synchronized method, this can
re-confirm that
                              //isAlive is "visible" to that
endless-loop-Thread
   }

}


just as a reference :-)  have a nice day :-)

Bo
Aug.06, 2001

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to