黑马程序员 日记一:传统定时器Timer及TimerTask。

来源:互联网 发布:sql常用查询语句大全 编辑:程序博客网 时间:2024/05/18 01:23

---------------------- android培训java培训、期待与您交流! ----------------------

类似于Thread类和Runnable接口,TimerTask触发实现的功能,必须重写run()方法。经过查阅API文档,可知,TimerTask类和Thread类一样,都实现了Runnable接口。

关于循环使用定时器:

张老师的视频中,循环实现定时器的代码如下:

public class TtaditionalTimerTest {

private static int count = 0;

public static void main (String[] args) {

class MyTimerTask extends TimerTask {

count = (count+1)%2;

System.out.println ("bombing!");

new Timer ().schedule (new MyTimerTask (), 2000+2000*count);

}

new Timer ().schedule (new MyTimerTask (), 2000);

while (true) {

try {

System.out.println (new Date ().getSeconds());

Thread.sleep (1000);

} catch (InterruptedException e){

e.printStackTrace ();

}

}

}

}

从这段代码中,学会使用TimerTimerTask的使用方法,已经循环使用定时器的一种方法。

但是由此产生的疑问:在main方法中定义了一个新类,在以往的经验中,虽然使用过内部类,但是都是在方法外定义的,不能定义在方法内。

解答:经过查阅资料,得知,如此定义的内部类称为方法内部类。

方法内部类有以下使用规则:

1.方法内部类只能在定义该内部类的方法内实例化,不能在其方法外进行实例化。

2.方法内部类对象不能使用该内部类所在方法的非final局部变量。

故在以上代码中,count变量只能用静态全局变量。

老师在视频中,提到使用两个TimerTask子类,实现循环定时器的效果,我自己实现另外一种循环使用定时器的方法:

public class TtaditionalTimerTest {

public static void main (String[] args) {

class MyTimerTask 1 extends TimerTask {

System.out.println ("bombing!");

new Timer ().schedule (new MyTimerTask2 (), 2000);

}

class MyTimerTask 2 extends TimerTask {

System.out.println ("bombing!");

new Timer ().schedule (new MyTimerTask 1(), 4000);

}

new Timer ().schedule (new MyTimerTask (), 2000);

while (true) {

try {

System.out.println (new Date ().getSeconds());

Thread.sleep (1000);

} catch (InterruptedException e){

e.printStackTrace ();

}

}

}

}

猜想:利用以上代码,实现一个利用两个方法内部类实现循环定时器。在该代码中,MyTimerTask1run方法中,new Timer()schedule方法中,使用一个MyTimerTask2,而在MyTimerTask2run方法中,new Timer()schedule方法中,使用一个MyTimerTask1,这样循环往复,就能实现循环计时。

但是在实际实验中,发现,在同一个方法中,定义两个方法内部类,第一个内部类调用第二个内部类时候,第二个内部类会出现无法实例化的错误。

所以,只能把两个TimerTask子类作为独立的类实现。如下:

主类

package com.itheima.beak;

import java.util.Date;

import java.util.Timer;

import java.util.TimerTask;

public class TimeerDemo {

/**

 * @param args

 */

//static int count = 0;

//static int[] times = {1000, 1000, 1000, 1000, 1000, 3000};

public static void main(String[] args) {

/*class MyTimerTask2 extends TimerTask {

//已经注释掉;

@Override

public void run() {

System.out.println ("bombing");

new Timer ().schedule(new MyTimerTask1(), 2000);

}

}

class MyTimerTask1 extends TimerTask {

@Override

public void run() {

System.out.println ("bombing");

new Timer ().schedule(new MyTimerTask2(), 4000);

}

}*/

new Timer ().schedule(new MyTimerTask1 (), 1000);

while (true) {

try {

System.out.println (new Date ().getSeconds());

Thread.sleep (1000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

第一个TimerTaskMyTimerTask1

package com.itheima.beak;

import java.util.Timer;

import java.util.TimerTask;

public class MyTimerTask1 extends TimerTask {

@Override

public void run() {

System.out.println ("get up!! now");

new Timer ().schedule(new MyTimerTask2(), 2000);

}

}

第二个TimerTaskMyTimerTask2

package com.itheima.beak;

import java.util.Timer;

import java.util.TimerTask;

public class MyTimerTask2 extends TimerTask {

@Override

public void run() {

System.out.println ("get up later!!");

new Timer ().schedule(new MyTimerTask1(), 4000);

}

}

自我扩展:

在老师的视频中,提到一个周一到周五能定时报时,而周六周日不报时的程序。不同于老师提到的quartz,我的实现如下:

static int[] times = {1000, 1000, 1000, 1000, 1000, 3000, 2000};

//依次为sundaymondaytuesdaywednesdaythursdayfridaySaturday距离下一次报时间隔的时间,为了方便验证,取值较小;

public static void main(String[] args) {

class MyTimerTask extends TimerTask {

@Override

public void run() {

System.out.println ("time to get up!!!"new Date ());

new Timer().schedule(new MyTimerTask (), times[Calendar.getInstance().get(Calendar.DAY_OF_WEEK)]); //从数组中取得下次报时的时间间隔

}

}

new Timer ().schedule(new MyTimerTask (), 1000);

while (true) {

try {

System.out.println (new Date ().getSeconds());

Thread.sleep (1000);

catch (InterruptedException e) {

e.printStackTrace();

}

}

}

---------------------- android培训java培训、期待与您交流! ----------------------

详细请查看:http://edu.csdn.net/heima