blog.Toby

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  130 随笔 :: 2 文章 :: 150 评论 :: 0 Trackbacks

web.xml

<servlet>
    <servlet-name>runthread</servlet-name>
    <servlet-class>com.investoday.servlet.RunThread</servlet-class>
    <load-on-startup>1</load-on-startup> <!--为1 表示服务启动就自动运行-->
  </servlet>
  <servlet-mapping>
    <servlet-name>runthread</servlet-name>
    <url-pattern>/runthread</url-pattern>
 </servlet-mapping>

TimeThread.java --线程类
class TimeThread extends Thread
---------------------------------------------------------
public void run()
    {
  try
        {
            for(;;)//无限循环
            {
                
               // some thing to do
                Thread.sleep(sleepTime);
            }
  } catch(Exception e)
        {
            e.printStackTrace();
  }

 }
----------------------------------------------------------------------------
RunThread.java  --调用线程的类
---------------------------------------
package com.investoday.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class RunThread extends HttpServlet
{
 public void service(HttpServletRequest req, HttpServletResponse res)
 throws ServletException, IOException
 {  
  try
  {
           TimeThread td = new TimeThread(1000);
            td.start();
  }
  catch(Exception ex)
  {
   ex.printStackTrace();
  }
 }

    public void init(ServletConfig config) throws ServletException
        {              
            TimeThread td = new TimeThread(10000);
            System.out.println("to run thread.");
            td.start();//执行 run 方法
        }

}

posted on 2005-12-28 15:09 渠上月 阅读(194) 评论(0)  编辑  收藏 所属分类: java tips

只有注册用户登录后才能发表评论。


网站导航: