在Java UI线程交互/ J2ME [英] Interacting with UI threads in Java/J2ME

查看:182
本文介绍了在Java UI线程交互/ J2ME的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个J2ME应用程序。其中一个件是什么,定期轮询一个目录的内容,并且,如果有任何新的东西,描绘在屏幕上。我通过让UI形式推出一个轮询线程的指针回到自己做到了这一点,当轮询线程发现一些它回调的​​形式和调用syncrhonized方法来更新它的显示。这似乎很好地工作。

I'm writing a J2ME application. One of the pieces is something that polls the contents of a directory periodically, and, if there are any new things, paints them on the screen. I've done this by having the UI form launch a polling thread with a pointer back to itself, and when the polling thread finds something it calls back to the form and calls a syncrhonized method to update it's display. This seems to work fine.

我的问题是这样的。在C#/。NET我知道这是不是最好有非UI线程更新UI,并处理这个正确的做法是委托它的UI线程。

The question I have is this. In C#/.NET I know it is not nice to have non-UI threads updating the UI, and the correct way to handle this is to delegate it up to the UI thread.

例如。以下内容:

public void DoSomeUIThing()
{
    if (this.uiComponent.InvokeRequired)
    {
        this.uiComponent.Invoke(someDelegateThatCallsBackToThis);
    }
    else
    {
        this.uiComponent.Text = "This is the update I want to happen";
    }
}

有一个J2ME相当于如何管理这一过程?如何Java的?还是的Java / J2ME只是对于这个更好的发挥?如果没有,这是怎么做的?

Is there a J2ME equivalent for how to manage this process? How about Java? Or does Java/J2ME just play nicer in regard to this? If not, how is this done?

看来,秋千支持就是我通过SwingUtilities.invokeLater()和invokeAndWait询问()方法。是否有J2ME的等效框架

It appears that Swing supports what I'm asking about via the SwingUtilities.invokeLater() and invokeAndWait() methods. Is there an equivalent framework for J2ME?

推荐答案

关于Java中,你所描述的像的 SwingWorker类(工作线程)

Regarding Java, what you are describing looks like a SwingWorker (worker thread).

当一个Swing程序需要执行长时间运行的任务,它通常采用工作线程,也被称为后台线程之一

When a Swing program needs to execute a long-running task, it usually uses one of the worker threads, also known as the background threads.

一个摇摆程序包括以下几种线程:

A Swing program includes the following kinds of threads:


  • 初始线程,即执行初始应用程序代码的线程

  • 事件调度线程,在那里执行所有的事件处理代码。与该摆动框架也必须在此线程执行交互最代码。

  • 工作线程,也被称为后台线程,其中费时后台任务被执行。

单线程规则

一旦Swing组件已经实现,所有代码都可能影响或依赖于应执行该组件的状态事件派发线程。

Single-thread rule:
Once a Swing component has been realized, all code that might affect or depend on the state of that component should be executed in the event-dispatching thread.

当在J2EE环境中使用,你需要的时候你的引用从EJB 一个SwingWorker的。

When used in a J2EE context, you need to be careful when you are referencing a SwingWorker from an EJB.

关于 < A HREF =htt​​p://developers.sun.com/mobility/midp/articles/blackberrydev/相对=nofollow> J2ME ,这取决于如果您正在开发应用程序作为标准的MIDlet将任何支持MIDP的设备上运行,或例如作为RIMlet,使用BlackBerry专用的API,因此只能在黑莓设备上运行一个基于CLDC的应用程序。

Regarding J2ME, it depends if you are developing your application as a standard MIDlet that will run on any MIDP-enabled device, or for instance as a RIMlet, a CLDC-based application that uses BlackBerry-specific APIs and therefore will run only on BlackBerry devices.

由于不像MIDP的UI类,RIM的都差不多在这个意义上,以摇摆的事件线程,它不是线程安全的,因为在MIDP上发生的UI操作。要在事件线程上运行代码,应用程序必须获得事件对象上的锁,或者使用的invokeLater()或invokeAndWait() - 额外的工作,为开发者,但精致带有一个价格标签

Because unlike MIDP's UI classes, RIM's are similar to Swing in the sense that UI operations occur on the event thread, which is not thread-safe as in MIDP. To run code on the event thread, an application must obtain a lock on the event object, or use invokeLater() or invokeAndWait() – extra work for the developer, but sophistication comes with a price tag.

但对于 LCDUI ,您可以从访问表单多线程的。

这篇关于在Java UI线程交互/ J2ME的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆