关于TimerTask与Timer类的应用
来源:才华咖 本文已影响1.3W人
来源:才华咖 本文已影响1.3W人
Timer类就像一个定时器,用于设置特定的时间或周期,来配合线程动作的执行,但是只调用Timer无法执行线程动作,必须配合另一个类TimerTask的方法才能使用。从示例6-13中可以发现Timer类与Thread类很相近。
public Timer ()
创建一个新的timer对象。
Timer属于计时器类,Time建立后,需要调用schedule()方法来执行计划任务,计划调度的方法有许多运用方式:
public void schedule (TimerTask task, long delay)
执行任务前先等待一段时间。task代表要执行的任务,delay代表需要等待的时间,单位为毫秒(1000毫秒为1秒)。
public void schedule (TimerTask task,
Date time)
在指定的系统时间time之后开始执行任务。
public void schedule (TimerTask task,
long delay,
long period)
延迟一段时间后开始执行任务,任务按照设定的周期执行。传入的.参数:delay为延迟时间,period为周期时间,单位均为毫秒。
public void schedule(TimerTask task,
Date firstTime,
long period)
在指定的时间后开始执行任务,任务按照设定的周期执行。传入的参数:firstTime为指定等待时间,period为周期时间,单位都是毫秒。
public void scheduleAtFixedRate (TimerTask task,
long delay,
long period)
执行任务内容后,等待delay时间,再按照period周期时间继续执行任务,单位都是毫秒。
public void scheduleAtFixedRate
(TimerTask task,
Date firstTime,
long period)
先等待一段时间firstTime,再执行计划任务,然后按照period周期时间继续执行任务,单位都是毫秒。
public void cancel()
停止timer计时。
有了Timer的各项调用方法,还不能直接执行计划任务,需要跟TimerTask类共同运用才能使计划任务开始运行。TimerTask所包含的相关方法如下:
public void TimerTask ()
创建一个新的TimerTask对象。
public abstract void run()
运行计划任务的工作内容。
public void cancel()
中止TimerTask类的执行流程,不再执行所调度的工作内容。
public void scheduleExcutionTime()
实际运行计划任务所用的时间。
示例6-13
/×
Timer和TimerTask类运行计划任务
×/
import et.×;
import i.×;
import .×;
public class threadDemo3 extends MIDlet
{
Display display;
canvasThread canvasthread;
public threadDemo3()
{
canvasthread=new canvasThread();
display=isplay(this);
}
public void startApp()
{
urrent(canvasthread);
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){}
public class canvasThread extends Canvas
{
int gameaction=0;
int view=0;
boolean again;
//声明一个Tim er类
Timer timers;
Image[] cloudImage;
Image[] sunImage;
public canvasThread()
{
timers=new Timer();
try{
cloudImage = new Image[]{
Image.createImage("/cloud-1.png"),
Image.createImage("/cloud-2.png"),
Image.createImage("/cloud-3.png"),
Image.createImage("/cloud-4.png")
};
sunImage = new Image[]{
Image.createImage("/sun-1.png"),
Image.createImage("/sun-2.png"),
Image.createImage("/sun-3.png"),
Image.createImage("/sun-4.png")
};
}catch(Exception ex){}
}
public void keyPressed(int keyCode)
{
gameaction=getGameAction(keyCode);
switch(gameaction)
{
case FIRE:
//运行计划任务
actionView();
break;
}
}
public void paint(Graphics g)
{
olor(0xFFFFFF);
Rect(0, 0, getWidth(), getHeight());
olor(0);
String("Timer线程动画应用", getWidth()/2, 20,
TER | );
Image(sunImage[view], getWidth()/2, 50, TER |
);
Image(cloudImage[view], getWidth()/2, 50, TER
| );
}
private void actionView()
{
/×
安排一个计划任务
运行任务内容:goCanvas()
delay时间:500毫秒
period周期:500毫秒
×/
dule(new goCanvas(), 500, 500);
}
//计划任务程序内容
class goCanvas extends TimerTask
{
public void run()
{
repaint();
if(again)
view--;
else
view++;
if(again && view==0)
again=false;
if(!again && view==3)
again=true;
}
}
}
}
【说明】本线程使用计划任务进行,所达到的效果跟使用线程的方式是相同的。只是在计划的内容上有所不同,例如不需要sleep()暂停时间,而是由schedule控制暂停时间。
英语阅读:African Union Leaders Talk Mali, Chinese Investmen
英语日常口语Unit14:Tim strikes it rich
六年级英语上册Unit2 New York is in the east of America说课稿
My favourite supermarket英语作文
Remarks by President Bush at CEO Summit
初中英语作文To Bea Computer Expert Is My Ideal
六下英语Unit5Ourearthlookslikethisfromspace教案
A Letter to the University President为题目的作文
Cortana makes a timid debut in Skype
英语作文:The Merits of Computer Games
《the most interesting stories》湘少版六下Unit7的相关知识及
英语阅读:VIT declines to acquire Ang Mo Kio property
should women work after getting married英语作文
MS SQL Server Management Studio Express安装图文具体步骤
At Climate Summit, Hope for Emissions Deal美文欣赏
The first day of term begins英语作文
Passage Eighteen (Strictly Ban smoking)英语阅读理解
关于类比推理教学方法在《细胞的生活》中的应用探索
外研一起2A M2 Unit 2 He doesnt like this shirt的优秀教案
RemarksofPresidentBarackObama优秀英语作文
The Importance of Breakfast英语作文
初中英语写作范文:My Happy Times During Winter Vacation
Unit4 Where is my car ? Part A 教学反思
中考英语阅读理解答案A report from the United Nations
九年级《unit 6 I like music that I can dance to. Period2》评课稿
Time To Stop Worrying About Invasive Species美文欣赏
Bob visits the village market实用的英语口语练习
英语作文: A letter to my sister
Unit 5 What are the shirts made of说课稿范文
关于TimerTask与Timer类的应用
Miss Australia wins Miss Universe title英语美文
How to make a fruit platter英语作文
The Role of Migrant Workers英语作文
关于物流服务与管理的应用
关于应用文写作基础知识:应用文分类
Module 6 Unit 1 I went there last year教学反思
My Happy Times During Winter Vacation初中英语作文
Module 7 Travel Unit 2 We are going to visit the Ming Tombs
I my borther and my sister初中英语作文
I Can’t Wait For Summer to Come英语作文