如果要在Web中使用监视线程,
可以在Bean中实现,这样就不会因为页面关闭而退出。

解决方案 »

  1.   

    不会吧,我在 servlet中的线程用得很好的,不会死掉。
      

  2.   

    在servlet中当然没事了,但在JSP中实现又是另一回事
      

  3.   

    //Watch.java
    package test;public class Watch extends Thread{
      private static Watch instance;
      private Watch() {
      }
      public static synchronized Watch getInstance(){
        if(instance==null){
          instance = new Watch();
        }
        return instance;
      }
      public void run(){
        int i=0;
        while(true){
          i++;
          try{
            sleep(1000);
          }catch(InterruptedException ie){}
          System.out.println("count: "+i);
        }
      }
    }//test.jsp
    <%@ page language="java" %>
    <%@ page import ="test.*" %>
    <%
    Watch aWatch = Watch.getInstance();
    aWatch.start();
    %>
    <html>
    <head>
    <title></title>
    <body></body>
    </html>
      

  4.   

    我的代码就是跟这个基本是一样的,但是循环只运行了第一次。
    我怀疑生成页面后线程就自动结束了。是不是这样啊?
    什么叫做out.close?多谢多谢!!我实在是太菜了。
      

  5.   

    to  water_crusoe(丝瓜) 
    不会吧,webwing(kevin) 的代码把浏览器关了都不会死的,你打开控制台,可以看到不断地输出count: 1/2/3/4......
      

  6.   

    我是往日志件里面写的,你看我的程序:
    //jsp 里面  
    KPIReminder rmd = KPIReminder.createReminder();
        if (rmd!=null) {
          Thread threadRemind = new Thread(rmd);
          threadRemind.start();
    //class里面
    while (!finished){
            DebugUtil.output(i);
            i++;
            Date temp = new java.util.Date();
            Date today = new java.sql.Date(temp.getYear(),temp.getMonth(),temp.getDay());
            if ((today.before(startDate))||(today.after(evaDeadline))) break;
            else Thread.sleep(1000*60);
            }
          }
        }catch (Exception e){
          DebugUtil.output(e.toString());
        }
      }