0

Quick question ... Using J2ME (CLDC 1.1, MIDP-2.1) is it possible to sleep the Midlet for a period of time (not using threads)... For example:

public class myMidlet extends MIDlet{
    public void startApp() {
        /* Sleep for 10 seconds */

        /* The answer was: */
        try {
            Thread.sleep(time_ms);
        } catch (Exception e) {}
    }
...

I don't use Java all that much, and don't want to program threads just for a simple sleep.

Thanks in advance

Answer Summary

My lack of Java knowledge. Examples I saw using Thread.sleep() led me to believe it was only usable in a thread object spawned by the Midlet ... not the midlet itself. I didn't want to have to spool off the midlet logic into a thread to sleep it ... But now I know the midlet runs in the default thread :) Going to find that Java book I never read because I didn't think I would use the language ever

gnat
  • 6,213
  • 108
  • 53
  • 73
Aiden Bell
  • 28,212
  • 4
  • 75
  • 119

4 Answers4

6

I didn't understand whether you mean putting midlet in paused state or just stopping execution for specified time.

If it's the latter, actually I don't undesrtand, why you don't want to use Threads, this is no big deal. You just insert three following lines wherever you need:

try {
    Thread.sleep(10000);
} catch (Exception ex) {}

That's all, nothing too complicating.

Malcolm
  • 41,014
  • 11
  • 68
  • 91
  • Thanks :) Didn't occur to me that the MIDlet was executing as a main thread and therefore required Thread.sleep() ... The examples I saw online were used in spawned threads :S – Aiden Bell Jun 20 '09 at 15:24
  • Well, MIDlet is a normal Java application with main thread and everything. :) Method Thread.sleep() is static meaning it may be used anywhere. Just be careful if you use it in a thread, which is processing UI events (e.g commandAction() method of CommandListener). – Malcolm Jun 20 '09 at 15:30
2

I don't know the exact answer, but I also don't understand what's the problem with calling static method Thread.sleep(milliseconds) that "Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds" . Do you call this programming threads?

Dmitry Tashkinov
  • 1,976
  • 19
  • 16
  • 1
    *slaps own head* Wasn't aware that the main thread was accessed like any other for sleeping (don't java much) ... thanks! – Aiden Bell Jun 20 '09 at 15:23
1

I would go for Malcolm's approach since your thread may possibly throw an exception.

[...]and don't want to program threads just[...]

Uh, you'll have a hard time programming J2ME and trying to avoid threaded programming. If your app becomes just a bit more complicated, especially when using network connections you'll have to use threads. Moreover if some operation takes more than 2-3 seconds it's highly advisable to run it in a separate thread, possibly (contemporaneously) notifying the user about the ongoing work.

Btw, what I forgot. I've recently written a J2ME application for a university course. There I've constructed what I called "ExecutableTask" which allowed me to handle threads in a convenient and easy way. If you want to have a look at the source...Unfortunately you cannot browse it online in the Google repository due to some bug of Google's hosting solution (some name of my project my cause this).

Juri
  • 32,424
  • 20
  • 102
  • 136
  • Thanks Juri ... After trying it and getting the Uncaught exception thing I swapped accepted answer. Not trying to avoid threads ... it is just a simple app that I didn't realize had thread capability in the MIDlet :) – Aiden Bell Jun 20 '09 at 15:29
  • 1
    When I need a separate thread to avoid blocking main thread and I don't want to mess with new classes, I create an anonymous class which extends Thread and run it. Very convenient, especially in cases described by Juri: filesystem access or some network connections. – Malcolm Jun 20 '09 at 15:43
0

You can try using Object.wait(), Object.wait(long timeoutValue). Although I would not advise you to try and delay the main startApp() / system thread.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
kiks
  • 11
  • 1