J2ME: The use of timer (TimerTask) and its preliminary study

Source: Internet
Author: User
Tags requires

Recently completed a J2ME game, always want to write something to everyone ~ because before always in the request, now put J2ME knowledge-timer knowledge and I have a little experience-pay to everyone, content is not much but its use, I hope that the introduction of the J2ME developers help.

First of all, the basic use of the timer, the following excerpt from the Nokia Forum in an article:

Design Java applications for the 60 series platform-timer

The various timers allow a variety of applications to easily plan tasks without having to allocate threads. Using threads often requires more complex designs and requires more system resources, which are more luxurious for a variety of gadgets.

Create an example of a timer:

public class MyTask extends TimerTask
{
private int iCount;
public MyTask(int aStart)
{
super();
iCount = aStart;
}
public void run()
{
iCounter--;
System.out.println(“Counter is now ” + iCounter);
if (iCounter == 0)
cancel();
}
}

Build a timer and add MyTask to it:

MyTask myTask = new Mytask(50);
Timer myTimer = Timer();
MyTimer.schedule(myTask, 1000, 20000);

In this example, the MyTask run () method is invoked every 20 seconds.

does not guarantee that the timer task must be performed on time. Each timer task occurs sequentially. If one of the tasks takes a long time, the next task can only be executed after the current task completes. For a recurring task, it is best to ensure that the run () method is completed quickly.

More than one timer is possible, so you can assign tasks to several timers. However, use multiple timers with caution, because each timer only runs its own thread, and synchronization may be required.

The above is the basic use of the timer, for some simple timer operation to do so, directly to execute the code into the run. For a complex logical structure, you need to do something else when you want to execute the Run method multiple times. Here is a frame I use the timer, in fact, is an object-oriented application, relatively simple to understand, I hope to have a help, but also want to have different views of friends to communicate together.

Also have a class to inherit TimerTask:

import java.util.TimerTask;
public class GAMETask extends TimerTask {
private GameObject m_Object;
public GAMETask(GameObject object) {
m_Object = object;
}
public void run() {
m_Object.gameTask();
}
}

Then define a Gameobject interface:

public interface GameObject {
public abstract void gameTask();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.