javaweb线程和java定时器

声明:本文章仅基于个人粗略整理总结,如有不足之处,欢迎指出。

+++++++++++++++++++++++++++

1、当启动web时就开启线程。

在web.xml里添加:

<!-- 线程:完整包名+类名 -->
<listener>
	   <listener-class>com.irs.util.protocol.TimerUtilPolice</listener-class>
</listener> 

实现类:

public class TimerUtilPolice implements ServletContextListener {
	
	private PoliceRecordService policeRecordService;
 @Override  
    public void contextInitialized(ServletContextEvent arg0)  {  
    	policeRecordService=new PoliceRecordService();//实例化
        final long timeInterval =1000*6;//线程时间
 Runnable runnable = new Runnable() {  
    	  public void run() {
    	                  while (true) {  
               long l = System.currentTimeMillis();
                Date date = new Date(l);
                SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
                System.out.println(dateFormat.format(date));
                policeRecordService.insPoliceRecordSet();
                try {  
                    Thread.sleep(timeInterval);  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
            }
    	    }
        };  
        Thread thread = new Thread(runnable);  

    	thread.start();
}

  
    @Override  
    public void contextDestroyed(ServletContextEvent arg0) {  
        // TODO Auto-generated method stub  
          
    }        

2、为减少内存损耗,可用定时器代替普通线程。原则上定时器也是启动线程,但定时器的可以指定第一次启动的延迟时间,不但启动可以控制,取消也是可控的。

​
package com.irs.util.protocol;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.irs.service.police.PoliceRecordService;

public class TimerUtilPolice implements ServletContextListener {

	 TimerTask task = new TimerTask() {  
         @Override  
         public void run() { //任务 
        	 long l = System.currentTimeMillis();
             Date date = new Date(l);
             SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
             System.out.println(dateFormat.format(date));
         }  
     };  
    	 
    @Override  
    public void contextInitialized(ServletContextEvent arg0)  {  
    	policeRecordService=new PoliceRecordService();

      final long timeInterval =1000*6;//单位毫秒  
      Timer timer = new Timer();  
    
// 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间,第四个参数为时间单位秒 
      timer.scheduleAtFixedRate(task, 1, timeInterval,TimeUnit.SECONDS); 
     

    }      
    
    @Override  
    public void contextDestroyed(ServletContextEvent arg0) {  
        // TODO Auto-generated method stub  
          
    }        

}

​

常用时间单位包含:

TimeUnit.DAYS          //天
TimeUnit.HOURS         //时
TimeUnit.MINUTES       //分
TimeUnit.SECONDS       //秒
TimeUnit.MILLISECONDS  //毫秒 

在Time类和TimerTask类中都有一个cancel()方法.

 TimerTask类中的作用是:将自身从任务队列中清除,(一个Timer对象可以执行多个Timertask任务)

 Timer类中的作用是:将任务队列中的全部任务清空.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值