定时任务Timer
Posted 奋斗的一线码农
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了定时任务Timer相关的知识,希望对你有一定的参考价值。
一、Timer介绍
java.util.Timer
java.util.TimerTask
Timer是一个定时器类,通过该类可以为指定的定时任务进行配置。TimerTask类是一个定时任务类,该类实现了Runnable接口,而且是一个抽象类,如下所示:
public abstract class TimerTask implements Runnable
可以通过继承该类,来实现自己的定时任务。
Timer定时器实例有多种构造方法:
Timer()
创建一个新计时器。
Timer(boolean isDaemon)
创建一个新计时器,可以指定其相关的线程作为守护程序运行。
Timer(String name)
创建一个新计时器,其相关的线程具有指定的名称。
Timer(String name, boolean isDaemon)
创建一个新计时器,其相关的线程具有指定的名称,并且可以指定作为守护程序运行。
二、Timer方法
定时执行方法
1、在特定时间执行任务,只执行一次
public void schedule(TimerTask task,Date time)
2、在特定时间之后执行任务,只执行一次
public void schedule(TimerTask task,long delay)
3、指定第一次执行的时间,然后按照间隔时间,重复执行
public void schedule(TimerTask task,Date firstTime,long period)
4、在特定延迟之后第一次执行,然后按照间隔时间,重复执行
public void schedule(TimerTask task,long delay,long period)
参数:
delay: 延迟执行的毫秒数,即在delay毫秒之后第一次执行
period:重复执行的时间间隔
5、第一次执行之后,特定频率执行,与3同
public void scheduleAtFixedRate(TimerTask task,Date firstTime,long period)
6、在delay毫秒之后第一次执行,后按照特定频率执行
public void scheduleAtFixedRate(TimerTask task,long delay,long period)
方法名称schedule()和scheduleAtFixedRate()两者的区别
<1>schedule()方法更注重保持间隔时间的稳定:保障每隔period时间可调用一次
<2>scheduleAtFixedRate()方法更注重保持执行频率的稳定:保障多次调用的频率趋近于period时间,如果任务执行时间大于period,会在任务执行之后马上执行下一次任务
Timer注销
timer.cancel();
三、案例
1、特定时间后执行
public void schedule(TimerTask task,long delay)
参数:
task为:执行任务
delay:时间毫秒数
方法的含义:
在delay毫秒后执行任务task,只执行一次。
案例:
1分钟后同步数据。
同步任务:
package com.yank.framework.common; import java.util.TimerTask; public class SynchroTimerTask extends TimerTask { @Override public void run() { // TODO Auto-generated method stub System.out.println("Synchro data to other servers."); } }
定时任务:
package com.yank.framework.common; import java.util.Timer; import java.util.TimerTask; public class SynchroTest { public static void main(String[] args) { // TODO Auto-generated method stub TimerTask task = new SynchroTimerTask(); Timer timer = new Timer(); timer.schedule(task, 1000); } }
2、案例2:按点吃饭
首先定义吃饭的任务,制定饭点,没小时进行检查,到点就吃饭。
package com.yank.framework.common; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import java.util.TimerTask; /* * 定时吃饭 * */ public class EatTimerTask extends TimerTask { //吃饭时间 private static List<Integer> eatTimes; /* * 静态初始化 * */ static { initEatTimes(); } /* * 初始化吃饭时间 * */ private static void initEatTimes(){ eatTimes = new ArrayList<Integer>(); eatTimes.add(8); eatTimes.add(12); eatTimes.add(18); } /* * 执行 * */ @Override public void run() { // TODO Auto-generated method stub Calendar calendar = Calendar.getInstance(); System.out.println("检查是否到了吃饭的点"); int hour = calendar.get(Calendar.HOUR_OF_DAY); if(eatTimes.contains(hour)) { System.out.println("饿了,吃饭..."); } } }
定时检查执行:
package com.yank.framework.common; import java.util.Calendar; import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class EatTimerTaskTest { public static void main(String[] arg){ TimerTask task = new EatTimerTask(); Calendar calendar= Calendar.getInstance(); Date firstTime = calendar.getTime(); //间隔:1小时 long period = 1000 * 60 * 60; //测试时间每分钟一次 //period = 1000 * 60; Timer timer = new Timer(); timer.schedule(task, firstTime, period); } }
my:
import Model.Trans.UserTransEntrustBuyDayList;
import Model.Trans.UserTransEntrustList;
import Model.Trans.UserTransEntrustSellDayList;
import Model.Trans.UserTransFinishlList;
import Model.Trans.UserTransList;
import Model.Trans.UserTransStatList;
import RssEasy.Core.DateTimeExtend;
import RssEasy.Core.FileExtend;
import RssEasy.Core.RedisExtend;
import RssEasy.DAL.mysqlHelper;
import RssEasy.MySql.Fund.CapitalFreezeList;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* 撮合交易
* @author Administrator
*/
public class AutoTrans implements ServletContextListener {
/**
*
*/
public static Timer autotimer;
/**
*
* @param sce
*/
@Override
public void contextInitialized(ServletContextEvent sce) {
if (autotimer != null) {
return;
}
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0); //凌晨0点
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 1);
ServletContext context = sce.getServletContext();
String pathinit = context.getRealPath("/") + "data\\\\transinit.txt"; //初始化
String patherr = context.getRealPath("/") + "data\\\\transerror.txt"; //错误
String pathlog = context.getRealPath("/") + "data\\\\translog.txt"; //交易
new Timer().schedule(new TimerTask() {
@Override
public void run() {
try {
Calendar curtime = Calendar.getInstance();
if (curtime.get(Calendar.HOUR_OF_DAY) != 0) {
return;
}
FileExtend.Write(pathinit, DateTimeExtend.format() + "init\\r\\n", true);
UserTransEntrustList entrust = new UserTransEntrustList(context);
entrust.keyvalue("state", 3);
entrust.update().where("shijian>? and weichengjiaoliang>0", String.valueOf(DateTimeExtend.date())).submit();
UserTransEntrustBuyDayList buyday = new UserTransEntrustBuyDayList(context);
buyday.select().query();
//解冻资金
while (buyday.for_in_rows()) {
CapitalFreezeList.unfreeze(context, 3, Long.parseLong(buyday.get("id")));
}
buyday.delete().submit();
UserTransEntrustSellDayList sellday = new UserTransEntrustSellDayList(context);
sellday.delete().submit();
//清除每天的数据
UserTransStatList stat = new UserTransStatList(context);
stat.columnvalue("available", "totalamout-selled-freeze-consume").columnvalue("amount", "totalamout-selled-consume").keyvalue("entrusted", "0");
stat.update().where("entrusted>0").submit();
//明星表
UserTransList trans = new UserTransList(context);
trans.columnvalue("shoupanjia", "zuixinjia");
trans.update().submit();
trans.select("myid,zuixinjia").query();
RedisExtend redis = RedisExtend.factory(context);
//jedis
while (trans.for_in_rows()) {
redis.delete("trans" + trans.get("myid") + "minute"); //删除分缓存
redis.delete("trans" + trans.get("myid") + "config"); //删除即时交易数据缓存
redis.delete("buy" + trans.get("myid")); //删除五档数据
redis.delete("sell" + trans.get("myid")); //删除五档数据
UserTransList.shenjia(context, trans.get("myid"), Double.parseDouble(trans.get("zuixinjia")));
}
} catch (Exception e) {
FileExtend.Write(patherr, e.toString() + DateTimeExtend.format() + "init\\r\\n", true);
}
}
}, calendar.getTime(), 24 * 60 * 60 * 1000);//一天后执行
autotimer = new Timer();
autotimer.schedule(new TimerTask() {
@Override
public void run() {
try {
Calendar curtime = Calendar.getInstance();
if (curtime.get(Calendar.DAY_OF_WEEK) == 0) {
return;
}
long transtime = curtime.getTimeInMillis() / 1000;
curtime.set(Calendar.HOUR_OF_DAY, 9);
curtime.set(Calendar.MINUTE, 30);
curtime.set(Calendar.SECOND, 0);
long starttime = curtime.getTimeInMillis() / 1000; //9:30的时间戳
double weituojia = 0;
//申购处理:在9:25~9:30
MySqlHelper buy = new MySqlHelper(context, "user_trans_entrust_buy_day_list");
buy.pagesize = 30;
UserTransFinishlList finish = new UserTransFinishlList(context);
/*if (transtime > starttime - 5 * 60 && transtime < starttime) {//9:25--9:30
UserTransList trans = new UserTransList(context);
buy.select().limit().query();
double fee = 0;
while (buy.for_in_rows()) {
int chengjiaoliang = Integer.parseInt(buy.get("weituoliang")); //委托量
buy.delete().where("id=?", buy.get("id")).submit();
if (trans.select("shengyushijian,faxingjia").where("myid=? and faxingjia<=" + buy.get("weituojia") + " and shengyushijian>=" + chengjiaoliang, buy.get("transmyid")).get_first_rows() == false) {
buy.append().submit();
continue;
}
//修改明星的剩余时间
trans.columnvalue("shengyushijian", "shengyushijian-" + chengjiaoliang);
trans.update().where("myid=?", buy.get("transmyid")).submit();
//交易成功记录表
weituojia = Double.parseDouble(trans.get("faxingjia"));
fee = weituojia * chengjiaoliang;
finish.keyvalue("myid", buy.get("myid")).keyvalue("transmyid", buy.get("transmyid")).keyvalue("chengjiaojia", weituojia).keyvalue("chengjiaoliang", chengjiaoliang).keyvalue("totalprice", fee).keyvalue("entrustid", buy.get("id")).timestamp();
finish.append().submit();
//解冻
CapitalFreezeList.unfreeze(context, 3, Integer.parseInt(buy.get("id")));
}
return;
}*/
//transtime < starttime || transtime > starttime + 11.5 * 3600 || (transtime > starttime + 2 * 3600 && transtime < starttime + 3.5 * 3600) || (transtime > starttime + 5.5 * 3600 && transtime < starttime + 8.5 * 3600)
//未到交易时间
if (transtime < starttime || transtime > starttime + 11 * 3600) {
return;
}
//开始交易
FileExtend.Write(pathlog, DateTimeExtend.format() + "start\\r\\n", true);
buy.select().orderby("weituojia desc").limit().query();
MySqlHelper sell = new MySqlHelper(context, "user_trans_entrust_sell_day_list"); //今日卖方
MySqlHelper entrust = new MySqlHelper(context, "user_trans_entrust_list"); //用户委托列表
MySqlHelper entity = new MySqlHelper(context, "user_trans_entrust_sell_day_list"); //动态
int buyamount = 0, sellamount = 0, transamount = 0;
while (buy.for_in_rows()) {
buyamount = Integer.parseInt(buy.get("weituoliang")); //未成交量
if (sell.select().where("transmyid=? and weituojia<=?", buy.get("transmyid"), buy.get("weituojia")).orderby("weituojia").query()) {//委托价<=买入价的 卖出委托列表
FileExtend.Write(pathlog, buy.toJson() + "\\r\\n", true);
while (sell.for_in_rows()) {
//TODO 购买量为零是否要return
weituojia = Double.parseDouble(sell.get("weituojia"));
sellamount = Integer.parseInt(sell.get("weituoliang"));
//修改 委托卖出数据(卖出委托表)
entity.tablename = "user_trans_entrust_sell_day_list";
if (buyamount >= sellamount) {
transamount = sellamount;
entity.delete().where("id=?", sell.get("id")).submit();
} else {
transamount = buyamount;
entity.columnvalue("weituoliang", "weituoliang-" + transamount);
entity.update().where("id=?", sell.get("id")).submit();
}
//更新卖方记录(委托记录表)
entrust.columnvalue("yichengjiaoliang", "yichengjiaoliang+" + transamount).columnvalue("weichengjiaoliang", "weichengjiaoliang-" + transamount);
entrust.update().where("id=?", sell.get("id")).submit();
//增加到卖方交易成功记录表
finish.keyvalue("myid", sell.get("myid")).keyvalue("transmyid", sell.get("transmyid")).keyvalue("chengjiaoliang", transamount).keyvalue("totalprice", weituojia * transamount).timestamp().keyvalue("entrustid", sell.get("id")).keyvalue("chengjiaojia", weituojia).keyvalue("typeid", "1");
finish.append().submit();
//增加到买方交易成功记录表
finish.keyvalue("myid", buy.get("myid")).keyvalue("transmyid", buy.get("transmyid")).keyvalue("chengjiaoliang", transamount).keyvalue("totalprice", weituojia * transamount).timestamp().keyvalue("entrustid", buy.get("id")).keyvalue("chengjiaojia", weituojia).keyvalue("typeid", "0");
finish.append().submit();
buyamount -= transamount;
if (buyamount <= 0) {//TODO 如果要分几次才能交易成功,资金要到最后才能全部解冻。
buyamount = 0;
CapitalFreezeList.unfreeze(context, 3, Integer.parseInt(buy.get("id")));
}
//更新买方记录(委托记录表)
entrust.columnvalue("yichengjiaoliang", "yichengjiaoliang+" + transamount).columnvalue("weichengjiaoliang", buyamount);
entrust.update().where("id=?", buy.get("id")).submit();
}
} else if (buy.pagerows < buy.pagesize) {
continue;
}
//添加购买委托记录(后边循环继续交易)要先删除购买委托记录
entity.tablename = "user_trans_entrust_buy_day_list";
entity.delete().where("id=?", buy.get("id")).submit();
if (buyamount > 0) {
buy.keyvalue("weituoliang", buyamount);
entity.keyvalue(buy);
entity.append().submit();
}
}
FileExtend.Write(pathlog, DateTimeExtend.format() + "end\\r\\n", true);
} catch (Exception ex) {
FileExtend.Write(patherr, ex.toString() + DateTimeExtend.format() + "\\r\\n", true);
}
}
}, 0, 5000);
}
/**
*
* @param sce
*/
@Override
public void contextDestroyed(ServletContextEvent sce) {
if (autotimer != null) {
autotimer.cancel();
autotimer = null;
}
}
}
2:
import Model.Trans.UserTransEntrustBuyDayList;
import Model.Trans.UserTransEntrustList;
import Model.Trans.UserTransEntrustSellDayList;
import Model.Trans.UserTransEntrustSubscribebuyDayList;
import Model.Trans.UserTransFinishlList;
import Model.Trans.UserTransList;
import Model.Trans.UserTransStatList;
import Model.Trans.UserTransSubscribebuyDayView;
import Model.Trans.UserTransSubscribebuyView;
import RssEasy.Core.DateTimeExtend;
import RssEasy.Core.FileExtend;
import RssEasy.Core.HttpRequestHelper;
import RssEasy.Core.RedisExtend;
import RssEasy.DAL.MySqlHelper;
import RssEasy.DAL.SqlHelper;
import RssEasy.MySql.Fund.CapitalFreezeList;
import RssEasy.MySql.User.UserList;
import util.TimeUtil;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* 撮合交易
* @author Administrator
*/
public class AutoTrans2 implements ServletContextListener {
/**
*
*/
public static Timer autotimer;
public Date startTime;
/**
*
* @param sce
*/
@Override
public void contextInitialized(ServletContextEvent sec) {
if (autotimer != null) {
return;
}
ServletContext context = sec.getServletContext();
String pathinit = context.getRealPath("/") + "data\\\\transinit.txt"; //初始化
String patherr = context.getRealPath("/") + "data\\\\transerror.txt"; //错误
String pathlog = context.getRealPath("/") + "data\\\\translog.txt"; //交易
try {
//申购时间到后10分钟后开始执行定时任务
MySqlHelper userTransList = new MySqlHelper(context, "user_trans_list_view");
userTransList.select("myid,ifnull(shengoushijian,0)as shengoushijian").where("isfaxing =0").get_first_rows();
String time = userTransList.get("shengoushijian");
Long time1=Long.parseLong(time)*1000+1000*60*10;
startTime = new Date(time1);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
autotimer = new Timer();
//申购任务
autotimer.schedule(new TimerTask() {
@Override
public void run() {
try {
//用户交易列表
UserTransList trans = new UserTransList(context);
//用户交易记录表
UserTransFinishlList finish = new UserTransFinishlList(context);
//申购详情表
MySqlHelper buyView = new MySqlHelper(context, "user_trans_subscribebuy_view_list");
//申购表
MySqlHelper subscribebuyTotal = new MySqlHelper(context, "user_trans_entrust_subscribebuy_day_list");
//所有用户申购的总的时间
subscribebuyTotal.select("ifnull(sum(weituoliang)*100,0) as totalTime").where("state = 0").get_first_rows();
int totalTime = Integer.parseInt(subscribebuyTotal.get("totalTime"));
//发行时间
MySqlHelper userTransList = new MySqlHelper(context, "user_trans_list");
userTransList.select().get_first_rows();
int liutongshijian = Integer.parseInt(userTransList.get("liutongshijian"));
//申购总时间和发行时间比较
if (totalTime <= liutongshijian) {//全部中签
//申购表数据
subscribebuyTotal.select().where("state = 0").query();
while(subscribebuyTotal.for_in_rows()) {
if (trans.select().where("myid=? ", subscribebuyTotal.get("transmyid")).get_first_rows() == false) {
continue;
}
//修改申购详情表的状态为 1
buyView.keyvalue("state","1");
buyView.update().where("state = 0 and relationid = ?",subscribebuyTotal.get("id")).submit();
//修改申购表的中签数量
subscribebuyTotal.remove("totalTime");
subscribebuyTotal.keyvalue("successnum", subscribebuyTotal.get("weituoliang"));
subscribebuyTotal.keyvalue("state","1");
subscribebuyTotal.update().where("id=?",subscribebuyTotal.get("id")).submit();
//增加到买方交易成功记录表
Double totalprice=Double.parseDouble(subscribebuyTotal.get("weituojia")) * Integer.parseInt(subscribebuyTotal.get("successnum"))*100;
finish.keyvalue("myid", subscribebuyTotal.get("myid"))
.keyvalue("transmyid", subscribebuyTotal.get("transmyid"))
.keyvalue("chengjiaoliang", Integer.parseInt(subscribebuyTotal.get("successnum"))*100+"")
.keyvalue("totalprice",totalprice)
.timestamp()
.keyvalue("entrustid", subscribebuyTotal.get("id"))
.keyvalue("chengjiaojia", subscribebuyTotal.get("weituojia"))
.keyvalue("typeid", "0");
finish.append().submit();
// 解冻资金 2:提现 3:购买 7:申购
CapitalFreezeList.unfreeze(context,subscribebuyTotal.get("myid"),totalprice, 7, Long.parseLong(subscribebuyTotal.get("id")));
//写入日志
FileExtend.Write(pathlog, subscribebuyTotal.toJson() + DateTimeExtend.format() + "\\r\\n", true);
}
// 修改明星的剩余时间
trans.columnvalue("shengyushijian", "shengyushijian-" + totalTime);
trans.update().where("myid=?", subscribebuyTotal.get("transmyid")).submit();
}
else {// 随机中签
// 申购详情表
MySqlHelper buyViewList = new MySqlHelper(context, "user_trans_subscribebuy_view_list");
// 申购表
UserTransEntrustSubscribebuyDayList buydayList = new UserTransEntrustSubscribebuyDayList(context);
// 用户表
//MySqlHelper userList = new MySqlHelper(context, "user_list");
// 申购详情视图
UserTransSubscribebuyDayView buydayView=new UserTransSubscribebuyDayView(context);
buydayView.select().where("state = 0").query();
// 把申购数据放到map以上是关于定时任务Timer的主要内容,如果未能解决你的问题,请参考以下文章