JDK中的Timer和TimerTask
2017-07-20 09:08
405 查看
点击打开链接
Timer是jdk中提供的一个定时器工具,使用的时候会在主线程之外起一个单独的线程执行指定的计划任务,可以指定执行一次或者反复执行多次。
TimerTask是一个实现了Runnable接口的抽象类,代表一个可以被Timer执行的任务。
一个Timer调度的例子
运行之后,在console会首先看到:
About to schedule task.
Task scheduled.
然后3秒钟后,看到
Time's up!
__________________________________________________________________________________________________________________
Timer timer = new Timer();
OrgMerchantMsgTimerTask task = new OrgMerchantMsgTimerTask(timer, merList);
// 延迟0毫秒开始任务,间隔100毫秒重发一次
timer.schedule(task, 0, 100);
/**
* 定时发送消息任务
*
* @author lijinyang
*/
private class OrgMerchantMsgTimerTask extends TimerTask {
private Timer timer;
private String url = "http://127.0.0.1:****/olt/skcb/callBackNotice?merchOrderId=";
private List<Map<String, Object>> merList;
public OrgMerchantMsgTimerTask(Timer timer,
List<Map<String, Object>> merList) {
this.timer = timer;
this.merList = merList;
}
@Override
public void run() {
if (merList.isEmpty()) {
timer.cancel();
} else {
Map<String, Object> aTran = merList.get(0);
String noticeUrl = url + aTran.get("trans_no");
try {
log.error("----向下游推送异步消息, {}-----", noticeUrl);
HttpUtils.httpGetMethod(noticeUrl, "utf-8");
} catch (Exception e) {
log.error("----向下游推送异步消息失败, {}-----", noticeUrl);
}
merList.remove(aTran);
}
}
}
Timer是jdk中提供的一个定时器工具,使用的时候会在主线程之外起一个单独的线程执行指定的计划任务,可以指定执行一次或者反复执行多次。
TimerTask是一个实现了Runnable接口的抽象类,代表一个可以被Timer执行的任务。
一个Timer调度的例子
1 import java.util.Timer; 2 import java.util.TimerTask; 3 4 public class TestTimer { 5 6 public static void main(String args[]){ 7 System.out.println("About to schedule task."); 8 new Reminder(3); 9 System.out.println("Task scheduled."); 10 } 11 12 public static class Reminder{ 13 Timer timer; 14 15 public Reminder(int sec){ 16 timer = new Timer(); 17 timer.schedule(new TimerTask(){ 18 public void run(){ 19 System.out.println("Time's up!"); 20 timer.cancel(); 21 } 22 }, sec*1000); 23 } 24 } 25 }
运行之后,在console会首先看到:
About to schedule task.
Task scheduled.
然后3秒钟后,看到
Time's up!
__________________________________________________________________________________________________________________
另一个栗子
List<Map<String, Object>> merList = tranService.transExQuery(params);Timer timer = new Timer();
OrgMerchantMsgTimerTask task = new OrgMerchantMsgTimerTask(timer, merList);
// 延迟0毫秒开始任务,间隔100毫秒重发一次
timer.schedule(task, 0, 100);
/**
* 定时发送消息任务
*
* @author lijinyang
*/
private class OrgMerchantMsgTimerTask extends TimerTask {
private Timer timer;
private String url = "http://127.0.0.1:****/olt/skcb/callBackNotice?merchOrderId=";
private List<Map<String, Object>> merList;
public OrgMerchantMsgTimerTask(Timer timer,
List<Map<String, Object>> merList) {
this.timer = timer;
this.merList = merList;
}
@Override
public void run() {
if (merList.isEmpty()) {
timer.cancel();
} else {
Map<String, Object> aTran = merList.get(0);
String noticeUrl = url + aTran.get("trans_no");
try {
log.error("----向下游推送异步消息, {}-----", noticeUrl);
HttpUtils.httpGetMethod(noticeUrl, "utf-8");
} catch (Exception e) {
log.error("----向下游推送异步消息失败, {}-----", noticeUrl);
}
merList.remove(aTran);
}
}
}
import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.commons.httpclient.HttpClient; public static String httpGetMethod(String url, String charset) throws Exception { HttpClient client = new HttpClient(); client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset); GetMethod getMethod = null; try { getMethod = new GetMethod(url); System.out.println("---开始向--"+url+"推送结果-----"); int statusCode = client.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { String msg = "访问失败!!HTTP_STATUS=" + statusCode; System.out.println(msg); System.out.println("---向"+url+"推送消息失败---"); return null; }// if String context = getMethod.getResponseBodyAsString(); return context; } finally { if (getMethod != null) getMethod.releaseConnection(); }// finally }// method
相关文章推荐
- JDK中的Timer和TimerTask详解
- JDK中的Timer和TimerTask详解
- JDK中的Timer和TimerTask详解 目录结构: Timer和TimerTask 一个Timer调度的例子 如何终止Timer线程 关于cancle方式终止线程 反复执行一个任务 sche
- jdk 中的timer 和timertask
- Timer和TimerTask
- 定时任务:Java中Timer和TimerTask的使用
- java_Timer和TimerTask
- Java线程(四):Timer和TimerTask
- Timer和TimerTask
- 定时任务:Java中Timer和TimerTask的使用
- Java定时器(一)Timer和TimerTask
- 任务调度(一)——jdk自带的Timer
- Timer 与 TimerTask 类
- 30-Timer和TimerTask
- Timer 和TimerTask 的定时任务入门
- 计时器 Timer,计时任务 TimerTask
- Java中使用Timer和TimerTask实现多线程
- Timer,TimerTask,Handler
- 使用Java中的Timer和TimerTask
- Java中使用Timer和TimerTask实现多线程