|
|
| J2ME Timer 使用指南 |
作者:
文章来源:
访问次数:67次
加入时间:2007年03月21日
|
|
<TABLE class=tf width="98%" align=center border=0> <TBODY> <TR> <TD class=bw width="100%"><FONT class=htd id=font_word style="FONT-SIZE: 14px; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif"><FONT size=2>出处 <IMG src="http://www.matrix.org.cn/images/small/url.gif" align=absMiddle></FONT><A href="http://wireless.java.sun.com/midp/ttips/timers/" target=_blank><FONT size=2>http://wireless.java.sun.com/midp/ttips/timers/</FONT></A><BR><BR><FONT size=2>J2SE 1.3 里有一项新的改进,那就是提供了一个可以更简单的实现多任务调度执行的定时器类,调度由一个后台线程完成。 MIDP 同样也包含了这一改进,使得 J2ME 开发人员从中受益。<BR><BR>J2ME 提示了两个类用来定义和调试任务, 他们分别是 TimerTask 和 Timer。TimerTask 是用户定义的需要被调度的所有任务的抽象基类。Timer 类在任务执行的时候负责创建和管理执行线程。<BR><BR>要定义一个任务,定义一个 TimerTask 的子类,并实现 run 方法。例如</FONT></FONT></TD></TR></TBODY></TABLE> <TABLE style="TABLE-LAYOUT: fixed" cellSpacing=0 borderColorDark=#ffffff cellPadding=4 width="98%" align=center bgColor=#e6e6e6 borderColorLight=#009ace border=1> <TBODY> <TR> <TD style="WORD-WRAP: break-word"><FONT size=2>import java.util.*;<BR><BR>public class MyTask extends TimerTask<BR>{<BR> public void run()<BR> {<BR> System.out.println( "Running the task" );<BR> }<BR>}.</FONT></TD></TR></TBODY></TABLE> <P><FONT size=2><FONT face=宋体> 是不是觉得 run 方法很熟悉呢?那是因为 TimerTask 实现了 java.lang.Runnable 接口。 Timer 类调用这个 run 方法来执行各个任务。此外还有一点必须注意到,那就是每个 run 方法所执行的任务必须能够尽快的终止,因为每个 Timer 对象在同一时间只能执行一个任务。<BR><BR> 定义好一个任务以后,你可以生成一个 Timer 对象并调用 schedule 方法来调度它,就像下面的代码演示的那样:</FONT><BR></FONT></P> <TABLE style="TABLE-LAYOUT: fixed" cellSpacing=0 borderColorDark=#ffffff cellPadding=4 width="98%" align=center bgColor=#e6e6e6 borderColorLight=#009ace border=1> <TBODY> <TR> <TD style="WORD-WRAP: break-word"><FONT size=2>import java.util.*;<BR><BR>Timer timer = new Timer();<BR>TimerTask task = new MyTask();<BR><BR>// 在执行这个任务前等待十秒...<BR>timer.schedule( task, 10000 );<BR><BR>// 在执行任务前等待十秒,然后每过十秒再执行一次<BR>timer.schedule( task, 5000, 10000 );</FONT></TD></TR></TBODY></TABLE> <P><FONT face=宋体 size=2>schedule 方法被重载了四次;每一个任务都可以在一个特定的时间点(使用一个 Date 对象指定)或者延时特定的时间段(以毫秒为单位)之后执行。你可以安排这个任务只执行一次或者在一段特定的时间段里反复执行。Timer 还提供了一个 scheduleAtFixedRate 方法来根据该任务第一次执行的时间来指定反复执行时延长的时间段。如果一个任务被延时了,被安排在后面执行的任务就被相应的缩短等待时间以“接上”被延时的任务。<BR><BR>每个 Timer 对象都会创建和管理一个后台线程。一般情况下,一个程序创建一个 Timer 就够了,当然也可以根据需要创建任意多个。你还可以在任何时候停止一个 Timer 并终止后台线程,方法是调用 cancel 方法。但要注意的是,一旦 Timer 并终止了,就不可能再恢复执行,除非你重新生成一个 Timer 对象并重新安排你想要执行的任务。Timer 对象是线程安全的,你可以在多线程的环境下直接访问 Timer 对象,而不用任何显式的同步处理。<BR><BR>另外,每个任务提供了一个 cancel 方法(继承自 TimerTask 基类),你可以在任务执行的过程当中调用该方法来终止该任务。一旦你终止了该任务,那么它将退出任务调度。你可以在任何时间调用每个任务的 cancel 方法来终止该任务的执行,哪怕该任务还一次都没有执行过。<BR><BR>下面提供了一个简示的 MIDlet 示例来演示 Timer 的使用,我们将利用定时器来模拟一个星空移动的效果。星星用一个点来表示,这使用到了低界图形 API。关于低界图形 API 更详细的介绍,请参考我的另一篇文章《使用 MIDP 的低界用户界面 API》。</FONT></P> <P> <TABLE style="TABLE-LAYOUT: fixed" cellSpacing=0 borderColorDark=#ffffff cellPadding=4 width="98%" align=center bgColor=#e6e6e6 borderColorLight=#009ace border=1> <TBODY> <TR> <TD style="WORD-WRAP: break-word"><FONT size=2><FONT face=宋体>import javax.microedition.midlet.*;<BR>import javax.microedition.lcdui.*;<BR>import java.util.*;<BR><BR>public class TimerDemo extends MIDlet {<BR><BR> Display display;<BR> StarField field = new StarField();<BR> FieldMover mover = new FieldMover();<BR> Timer timer = new Timer();<BR><BR> public TimerDemo() {<BR> display = Display.getDisplay( this );<BR> }<BR><BR> protected void destroyApp( boolean unconditional ) {<BR> }<BR><BR> protected void startApp() {<BR> display.setCurrent( field );<BR> timer.schedule( mover, 100, 100 );<BR> }<BR><BR> protected void pauseApp() {<BR> }<BR><BR> public void exit(){<BR> timer.cancel(); // stop scrolling<BR> destroyApp( true );<BR> notifyDestroyed();<BR> }<BR><BR> class FieldMover extends TimerTask {<BR> public void run(){<BR> field.scroll();<BR> }<BR> }<BR><BR> class StarField extends Canvas {<BR> int height;<BR> int width;<BR> int[] stars;<BR> Random generator = new Random();<BR> boolean painting = false;<BR><BR> public StarField(){<BR> height = getHeight();<BR> width = getWidth();<BR> stars = new int[ height ];<BR><BR> for( int i = 0; i < height; ++i ){<BR> stars[i] = -1;<BR> }<BR> }<BR><BR> public void scroll() {<BR> if( painting ) return;<BR><BR> for( int i = height-1; i > 0; --i ){<BR> stars[i] = stars[i-1];<BR> }<BR><BR> stars[0] = ( generator.nextInt() %<BR> ( 3 * width ) ) / 2;<BR> if( stars[0] >= width ){<BR> stars[0] = -1;<BR> }<BR><BR> repaint();<BR> }<BR><BR> protected void paint( Graphics g ){<BR> painting = true;<BR><BR> g.setColor( 0, 0, 0 );<BR> g.fillRect( 0, 0, width, height );<BR><BR> g.setColor( 255, 255, 255 );<BR><BR> for( int y = 0; y < height; ++y ){<BR> int x = stars[y];<BR> if( x == -1 ) continue;<BR><BR> g.drawline( x, y, x, y );<BR> }<BR><BR> painting = false;<BR> }<BR><BR> protected void keypressed( int keycode ){<BR> exit();<BR> }<BR> }<BR>}</FONT><BR></FONT></TD></TR></TBODY></TABLE></P> <P><FONT size=2><FONT face=宋体>TimerDemo MIDlet 使用了一个 Timer 对象 timer 来调度执行一个 TimerTask 任务 FieldMover,时间间隙 100 毫秒。FieldMover 处理星空的更新并重绘任务,使得整个星空不断得往屏幕下方“延伸”。这样就生成了一个简单的星空移动的效果。<BR><BR><B>关于翻译作者</B></FONT></FONT></P> <TABLE style="TABLE-LAYOUT: fixed" cellSpacing=0 borderColorDark=#ffffff cellPadding=4 width="98%" align=center bgColor=#e6e6e6 borderColorLight=#009ace border=1> <TBODY> <TR> <TD style="WORD-WRAP: break-word"><FONT size=2>稀饭,Java 爱好者.点击<IMG src="http://www.matrix.org.cn/images/small/url.gif" align=absMiddle></FONT><A href="http://www.matrix.org.cn/article.asp" target=_blank><FONT size=2>http://www.matrix.org.cn/article.asp</FONT></A><FONT size=2>查看更多翻译文章</FONT></TD></TR></TBODY></TABLE>
|
|
|