让 J2ME Midlet 在没有线程的情况下休眠?

发布于 2024-07-25 21:01:24 字数 638 浏览 1 评论 0原文

快速问题...使用J2ME(CLDC 1.1,MIDP-2.1)是否可以让Midlet休眠一段时间(不使用线程)...例如:

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

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

我不太使用Java,并且不使用Java不想仅仅为了简单的睡眠而对线程进行编程。

提前致谢

答案摘要

我缺乏Java知识。 我看到的使用 Thread.sleep() 的示例让我相信它只能在 Midlet 生成的线程对象中使用......而不是 midlet 本身。 我不想将 midlet 逻辑脱机到一个线程中以使其休眠...但现在我知道 midlet 在默认线程中运行:) 去找那本我从未读过的 Java 书,因为我没有读过我认为我永远不会使用这种语言

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

聽兲甴掵 2024-08-01 21:01:24

我不明白你的意思是让 midlet 处于暂停状态还是只是在指定的时间内停止执行。

如果是后者,其实我不明白,为什么你不想使用线程,这没什么大不了的。 您只需在需要的地方插入以下三行即可:

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

仅此而已,没有什么太复杂的。

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.

小巷里的女流氓 2024-08-01 21:01:24

我不知道确切的答案,但我也不明白调用静态方法 Thread.sleep(milliseconds) “导致当前执行的线程休眠(暂时停止执行)指定的毫秒数”有什么问题。 你称这种编程为线程吗?

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?

花开雨落又逢春i 2024-08-01 21:01:24

我会采用马尔科姆的方法,因为您的线程可能会抛出异常。

[...]并且不想对线程进行编程
只是[...]

呃,您将很难进行 J2ME 编程并试图避免线程编程。 如果您的应用程序变得稍微复杂一点,特别是在使用网络连接时,您将不得不使用线程。 此外,如果某些操作花费超过 2-3 秒,强烈建议在单独的线程中运行它,可能(同时)通知用户正在进行的工作。

顺便说一句,我忘了什么。 我最近为大学课程编写了 J2ME 应用程序。 在那里,我构建了所谓的“ExecutableTask”,它使我能够以方便且简单的方式处理线程。 如果您想查看源代码...不幸的是,您无法在 Google 存储库中在线浏览它,因为谷歌托管解决方案的一些错误(我的项目的某些名称导致了这一点)。

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).

过去的过去 2024-08-01 21:01:24

您可以尝试使用Object.wait()Object.wait(long timeoutValue)。 尽管我不建议您尝试延迟主 startApp() /系统线程。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文