JOptionPane.showMessageDialog线程安全吗? [英] Is JOptionPane.showMessageDialog thread safe?

查看:142
本文介绍了JOptionPane.showMessageDialog线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JOptionPane.showMessageDialog应该是一个有用的实用程序,用于获取用户反馈,因为它会在您等待时阻止当前线程。

JOptionPane.showMessageDialog is supposed to be a useful utility for getting user feedback as it blocks your current thread while you wait.

因此我希望它会是线程安全,你不需要在invokeLater或invokeAndWait中包装调用。

I would expect therefore that it would be thread-safe and that you wouldn't need to wrap the call in an invokeLater or an invokeAndWait.

这是这种情况吗?

推荐答案

取自javax.swing包描述:

Taken from the javax.swing package description:


Swing的线程策略



一般情况下,Swing不是线程安全的。除非另有说明,否则必须在事件派发线程上访问所有Swing组件和相关类。典型的Swing应用程序响应于从用户手势生成的事件进行处理。例如,单击JButton会通知添加到JButton的所有ActionListener。由于在事件调度线程上调度了从用户手势生成的所有事件,因此大多数开发人员不受限制的影响。

Swing's Threading Policy

In general Swing is not thread safe. All Swing components and related classes, unless otherwise documented, must be accessed on the event dispatching thread. Typical Swing applications do processing in response to an event generated from a user gesture. For example, clicking on a JButton notifies all ActionListeners added to the JButton. As all events generated from a user gesture are dispatched on the event dispatching thread, most developers are not impacted by the restriction.

然而,影响在于构建和显示Swing应用程序。不会在事件派发线程上调用对应用程序的主方法或Applet中的方法的调用。因此,在构造和显示应用程序或applet时,必须注意将控制转移到事件调度线程。传递控制并开始使用Swing的首选方法是使用invokeLater。 invokeLater方法调度要在事件分派线程上处理的Runnable。

Where the impact lies, however, is in constructing and showing a Swing application. Calls to an application's main method, or methods in Applet, are not invoked on the event dispatching thread. As such, care must be taken to transfer control to the event dispatching thread when constructing and showing an application or applet. The preferred way to transfer control and begin working with Swing is to use invokeLater. The invokeLater method schedules a Runnable to be processed on the event dispatching thread.

JOptionPane没有记录它是线程安全的,所以你必须使用 invokeLater()

JOptionPane does not document that it is thread safe, so you have to use invokeLater().

这篇关于JOptionPane.showMessageDialog线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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