Java实现定时器的四种方式
标签:timertask name sch long title import new t ram task
-
package com.wxltsoft.tool;
-
-
-
-
import java.util.Calendar;
-
-
-
import java.util.TimerTask;
-
-
-
-
-
-
-
-
-
-
-
public static void main(String[] args) {
-
-
-
-
-
-
-
-
-
-
public static void timer1(){
-
Timer nTimer = new Timer();
-
nTimer.schedule(new TimerTask() {
-
-
-
System.out.println("----设定要指定任务-----");
-
-
-
-
-
-
-
-
public static void timer2() {
-
Timer timer = new Timer();
-
timer.schedule(new TimerTask() {
-
-
System.out.println("-------延迟5000毫秒,每1000毫秒执行一次--------");
-
-
-
-
-
-
-
-
public static void timer3() {
-
Timer timer = new Timer();
-
timer.scheduleAtFixedRate(new TimerTask() {
-
-
System.err.println("-------延迟5000毫秒,每1000毫秒执行一次--------");
-
-
-
-
-
-
* java.util.Timer.scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
-
-
public static void timer4() {
-
Calendar calendar = Calendar.getInstance();
-
calendar.set(Calendar.HOUR_OF_DAY, 17);
-
calendar.set(Calendar.MINUTE, 26);
-
calendar.set(Calendar.SECOND, 0);
-
-
Date time = calendar.getTime();
-
-
Timer timer = new Timer();
-
timer.scheduleAtFixedRate(new TimerTask() {
-
-
System.out.println("-------设定要指定任务--------");
-
-
}, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行
-
-
-
-
Java实现定时器的四种方式
标签:timertask name sch long title import new t ram task
原文地址:https://www.cnblogs.com/niudaxianren/p/11077578.html
评论