在Java程序中使用多个线程,vs需要在EDT上创建Swing对象 [英] Use of multiple threads in a Java program and vs need to create Swing objects on EDT

查看:194
本文介绍了在Java程序中使用多个线程,vs需要在EDT上创建Swing对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Re:在事件发送线程上创建Swing对象的要求。

Re: Requirement to create Swing Object on Event-Dispatch Thread.

我正在处理一个应用程序,其目的是监视和显示各种远程嵌入式服务器我对Java很新,我对于Swing Objects和EDT的要求的理解是不完整的。

I am working on an application, the purpose of which is to monitor and display the condition of various remote embedded servers. I'm pretty new to Java, and my understanding of the requirement with respect to the Swing Objects and the EDT is incomplete.

主要的GUI是在EDT上启动的通常的方式如下,

The main GUI is started on the EDT in the usual fashion as follows,

    javax.swing.SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            createAndShowGUI();
        }
    });

然后,用户可以选择一个或多个对应于远程机器中的一个或多个的菜单选项。这样做的效果是每次创建一个新线程。

The user may then select one or more menu options corresponding to the one or other of the remote machines. The effect of this is to create a new thread each time as follows

                new Thread(new VoterStatus(itemNumber)).start();

它调用VoterStatus的类run方法,这又创建了一个带有JFrame的新窗口。新的线程,VoterStatus类的一个实例,然后询问(TCP等)特定的远程指定(itemNumber),收集各种信息并在JFrame中显示它们。

which invokes VoterStatus's class "run" method which in turn creates a new window with a JFrame. The new thread, an instance of VoterStatus class, then interrogates (TCP etc) the particular remote specified (itemNumber), collecting various bits of information and displaying them in the JFrame.

可能有任何数量的这样的线程对应于VoterStatus的实例,所有这些线程都更新自己的窗口。这些各种窗口/ JFrame /任务之间没有数据共享。

There may be any number of such threads corresponding to an instance of VoterStatus, all updating their own windows. There is no sharing of data between these various windows/JFrame/tasks.

这似乎工作正常,但是是安全的?

This seems to work just fine, but is is safe?

我是否违反了在EDT上创建Swing组件的规则?

Have I violated the rule about creating Swing components on the EDT?

将使用SwingWorker类有益的?

Would use of the SwingWorker class be beneficial?

感谢Java程序员在这些问题上更有经验的评论。

I would appreciate any comments from Java programmers more experienced in such matters.

感谢
Steve

Thanks Steve

推荐答案

从Swing教程中的事件调度线程

From the section in the Swing tutorial titled The Event Dispatch Thread

某些Swing组件方法已标记API规范中的线程安全这些可以从任何线程安全地调用。必须从事件分派线程调用​​所有其他Swing组件方法。忽略此规则的程序大部分时间可能正常运行,但会受到难以复制的不可预知的错误的影响。

我总是调用我的方法EDT,所以我不浪费时间追逐gremlins。

I always invoke my methods on the EDT so I don't waste time chasing gremlins.

编辑:

说明了线程安全的注释已经从JDK7 API中的许多方法中删除。 http://forums.oracle.com/forums/thread.jspa?threadID=2167051 。这看起来像另一个原因,确保影响GUI的所有方法都在EDT上执行。

I just read another posting which states that the comment "thread safe" has been removed from many methods in the JDK7 API. http://forums.oracle.com/forums/thread.jspa?threadID=2167051. This looks like another reason to make sure all methods that affect the GUI are executed on the EDT.

这篇关于在Java程序中使用多个线程,vs需要在EDT上创建Swing对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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