J2ME and java - threads

Conor026

Member
May 20, 2003
118
0
0
I`m developing a J2ME game for a college project.
I need a function to run a certain time e.g. every tenth of a second and threads are the only way to do this, i`m told.

but while I can create the thread in the class that extends from canvas with
Thread myThread=new Thread(this);

and then get the thread to run using
myThread.start(); or myThread.run();

and then get it to sleep with
myThread.sleep(100);

Is there anyway of getting the thread to run again itself after the sleep period is over or do you have to call the run() function every time you want it to run
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
your run method should look like:

public void run() {
//set some stuff up
//more setting up

while (running) {
//do work
//more work

//time to sleep
Thread.Sleep(100);
}

//do any clean up work
//more clean up
}

Its important for the while loop not to simply be while(true) because this boolean allows you to exit the thread gracefully. Set it to true before calling run and when you need to exit, set it to false and use thread.join() to wait for it to finish. At least that's the general java thread model, haven't fiddled with j2me, so there might be a need for some other special thing there.

Or the Timer, if that's available in j2me would work as well.