//问题一示例: m_timer.scheduleAtFixedRate(new TaskUseLongTime(), 1000, 5000); m_timer.scheduleAtFixedRate(new TaskNormal(), 5000, 3000); 运行结果: 14:44:29: timer is sleeping 10 seconds 14:44:39: Task Normal executed 14:44:39: timer is sleeping 10 seconds 14:44:49: Task Normal executed 14:44:49: Task Normal executed 14:44:49: timer is sleeping 10 seconds 结果分析:TaskNormal任务无法保证3秒运行一次,其只能等待TaskUseLongTime运行结束后才可以。
//问题二示例: m_timer.schedule(new TaskThrowException(), 1000); m_timer.schedule(new TaskNormal(), 2000); 运行结果: 14:47:37: Throw exception Exception in thread "Timer-0" java.lang.RuntimeException at timer_test.TimerTest$TaskThrowException.run(TimerTest.java:85) at java.util.TimerThread.mainLoop(Timer.java:512) at java.util.TimerThread.run(Timer.java:462) 结果分析: 当前一个任务抛出异常后,后面的TaskNormal任务无法继续运行
//问题三示例:
m_timer.scheduleAtFixedRate(new TaskUseLongTime("timer1"), 1000, 15000);
m_timer.scheduleAtFixedRate(new TaskUseLongTime("timer2"), 1000, 15000);
运行结果:
14:50:16: timer1 is sleeping 10 seconds
14:50:26: timer2 is sleeping 10 seconds
14:50:36: timer2 is sleeping 10 seconds
结果分析:
我的启动时间均是1秒以后,但是timer1和timer2启动的时间明显不一致
package timer_test;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class TimerTest
{
private final Timer m_timer = new Timer();
public static void main(String[] args)
{
new TimerTest().test();
}
public void test()
{
//问题一示例:
m_timer.scheduleAtFixedRate(new TaskUseLongTime(), 1000, 5000);
m_timer.scheduleAtFixedRate(new TaskNormal(), 5000, 3000);
//问题二示例:
// m_timer.schedule(new TaskThrowException(), 1000);
// m_timer.schedule(new TaskNormal(), 2000);
//问题三示例:
// m_timer.scheduleAtFixedRate(new TaskUseLongTime("timer1"), 1000, 5000);
// m_timer.scheduleAtFixedRate(new TaskUseLongTime("timer2"), 1000, 5000);
}
private class TaskUseLongTime extends TimerTask
{
private String m_taskName = "timer";
public TaskUseLongTime(){
}
public TaskUseLongTime(String taskName)
{
m_taskName = taskName;
}
@Override
public void run()
{
try
{
System.out.println(getCurrentTime()+": "+m_taskName+" is sleeping 10 seconds");
Thread.sleep(10000);
}
catch (InterruptedException e)
{
}
}
}
private class TaskNormal extends TimerTask
{
@Override
public void run()
{
System.out.println(getCurrentTime()+": Task Normal executed");
}
}
private class TaskThrowException extends TimerTask
{
@Override
public void run()
{
System.out.println(getCurrentTime()+": Throw exception");
throw new RuntimeException();
}
}
private String getCurrentTime()
{
return new SimpleDateFormat("HH:mm:ss").format(new Date());
}
}
//问题一: m_timer.scheduleAtFixedRate(new TaskUseLongTime(), 1000, 5000, TimeUnit.MILLISECONDS); m_timer.scheduleAtFixedRate(new TaskNormal(), 1000, 5000, TimeUnit.MILLISECONDS); 运行结果: 14:54:37: Task Normal executed 14:54:37: timer is sleeping 10 seconds 14:54:42: Task Normal executed 14:54:47: Task Normal executed 14:54:47: timer is sleeping 10 seconds 14:54:52: Task Normal executed
//问题二: m_timer.scheduleAtFixedRate(new TaskThrowException(), 1000, 5000, TimeUnit.MILLISECONDS); m_timer.scheduleAtFixedRate(new TaskNormal(), 1000, 5000, TimeUnit.MILLISECONDS); 运行结果: 14:58:36: Throw exception 14:58:36: Task Normal executed 14:58:41: Task Normal executed 14:58:46: Task Normal executed 14:58:51: Task Normal executed 14:58:56: Task Normal executed
//问题三:
m_timer.scheduleAtFixedRate(new TaskUseLongTime("timer1"), 1000, 5000, TimeUnit.MILLISECONDS);
m_timer.scheduleAtFixedRate(new TaskUseLongTime("timer2"), 1000, 5000, TimeUnit.MILLISECONDS);
运行结果:
15:01:12: timer1 is sleeping 10 seconds
15:01:12: timer2 is sleeping 10 seconds
15:01:22: timer2 is sleeping 10 seconds
15:01:22: timer1 is sleeping 10 seconds
15:01:32: timer1 is sleeping 10 seconds
15:01:32: timer2 is sleeping 10 seconds
package timer_test;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ScheduleThreadPoolExecutorTest
{
private final ScheduledThreadPoolExecutor m_timer = new ScheduledThreadPoolExecutor(10);
public static void main(String[] args)
{
ScheduleThreadPoolExecutorTest timerTest = new ScheduleThreadPoolExecutorTest();
timerTest.test();
try
{
Thread.sleep(100000);
}
catch (InterruptedException e)
{
}
finally
{
timerTest.shutdown();
}
}
public void shutdown()
{
m_timer.shutdown();
}
public void test()
{
//问题一:
// m_timer.scheduleAtFixedRate(new TaskUseLongTime(), 1000, 5000, TimeUnit.MILLISECONDS);
// m_timer.scheduleAtFixedRate(new TaskNormal(), 1000, 5000, TimeUnit.MILLISECONDS);
//问题二:
// m_timer.scheduleAtFixedRate(new TaskThrowException(), 1000, 5000, TimeUnit.MILLISECONDS);
// m_timer.scheduleAtFixedRate(new TaskNormal(), 1000, 5000, TimeUnit.MILLISECONDS);
//问题三:
m_timer.scheduleAtFixedRate(new TaskUseLongTime("timer1"), 1000, 5000, TimeUnit.MILLISECONDS);
m_timer.scheduleAtFixedRate(new TaskUseLongTime("timer2"), 1000, 5000, TimeUnit.MILLISECONDS);
}
private class TaskUseLongTime implements Callable<Integer>, Runnable
{
private String m_taskName = "timer";
private TaskUseLongTime(){
}
private TaskUseLongTime(String taskName)
{
m_taskName = taskName;
}
public void run()
{
try
{
System.out.println(getCurrentTime()+": "+m_taskName+" is sleeping 10 seconds");
Thread.sleep(10000);
}
catch (InterruptedException e)
{
}
}
public Integer call() throws Exception
{
run();
return 0;
}
}
@SuppressWarnings("unused")
private class TaskNormal implements Callable<Integer>, Runnable
{
public Integer call() throws Exception
{
run();
return 0;
}
public void run()
{
System.out.println(getCurrentTime()+": Task Normal executed");
}
}
@SuppressWarnings("unused")
private class TaskThrowException implements Callable<Integer>, Runnable
{
public Integer call() throws Exception
{
System.out.println(getCurrentTime()+": Throw exception");
throw new RuntimeException();
}
public void run()
{
System.out.println(getCurrentTime()+": Throw exception");
throw new RuntimeException();
}
}
private String getCurrentTime()
{
return new SimpleDateFormat("HH:mm:ss").format(new Date());
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有