java_Timer_schedule jdk自带定时器
2014-03-14 14:29
225 查看
定时器经常在项目中用到,定制执行某些操作,比如爬虫就需要定时加载种子等操作,之前一直用spring的定制器 近期做项目发现,jdk有很简单的提供 代码如下 1 /* * Copyright (c) 2014-2024 . All Rights Reserved. * * This software is the confidential and proprietary information of * LoongTao. You shall not disclose such Confidential Information * and shall use it only in accordance with the terms of the agreements * you entered into with LoongTao. * */ package com.loongtao.dmscrawler.temp; import java.util.Timer; import java.util.TimerTask; /** * @declare:测试定时器 <br> * @author: cphmvp * @version: 1.0 * @date: 2014-3-14上午11:21:36 */ public class TimeTest { public static void main(String[] args) { System.out.println("start"); new Timer().schedule(new TestTask(), 0, 1000 * 60 * 60); System.out.println("end"); } static class TestTask extends TimerTask { TestTask() { // 空构造器 } @Override public void run() { System.out.println(1); } } } jdk源码参考 很容易看懂的 ,不解释
* @param task task to be scheduled. * @param delay delay in milliseconds before task is to be executed. * @param period time in milliseconds between successive task executions. * @throws IllegalArgumentException if <tt>delay</tt> is negative, or * <tt>delay + System.currentTimeMillis()</tt> is negative. * @throws IllegalStateException if task was already scheduled or * cancelled, timer was cancelled, or timer thread terminated. */ public void schedule(TimerTask task, long delay, long period) { if (delay < 0) throw new IllegalArgumentException("Negative delay."); if (period <= 0) throw new IllegalArgumentException("Non-positive period."); sched(task, System.currentTimeMillis()+delay, -period); }
相关文章推荐
- java 自带 定时器Timer
- 两种方式实现java定时器,使用quartz定时器框架和java自带Timer定时器,编写定时任务
- Java多线程核心技术(五):定时器Timer
- java中使用线程实现Timer(定时器)原理和源码
- jdk自带的Timer定时器的使用示例
- Java定时器,Timer/quartz实践
- Java 中Timer和TimerTask 定时器和定时任务使用的例子
- Java基础:定时器Timer
- Spring定时器配置的两种实现方式OpenSymphony Quartz和java Timer详解
- Java Timer 定时器的使用
- java中定时器(java.util.Timer)
- java定时器的使用(Timer)
- 【Java多线程编程核心技术】5.定时器Timer-笔记总结
- java多线程--定时器Timer的使用
- java定时器的使用(Timer)
- java中Timer定时器的使用
- java中使用线程实现Timer(定时器)原理和源码
- java中使用线程实现Timer(定时器)原理和源码
- java多线程核心技术 第五章 定时器Timer
- java中的定时器Timer