我怎么知道我是否在事件发送线程上? [英] How do I know if I'm on the event dispatch thread?

查看:122
本文介绍了我怎么知道我是否在事件发送线程上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.考虑我的代码在我所拥有的JPanel的某些行上,我是否自动在EDT上?

1.Consider my code is on some line of a JPanel that I have, am I automatically on EDT?

2.对于不属于GUI,JPanels或其他视图类,简单逻辑类的所有其他类的同名问题。

2.Same question for all other classes which are not belong to GUI, JPanels or other view classes, simple logical class.

3.如果我有JPanel我正在播放音乐,那么音乐应该在事件发送线程上还是在非EDT的其他线程上运行(对于没有阻止GUI,虽然从EDT运行它没有任何问题)?

3.If I have JPanel which I'm playing music while being in it, should the music run on event dispatch thread or on other thread which is not EDT (for not blocking the GUI, although I didn't feel any problem with running it from EDT)?

注意:我想要一般规则如何知道它而不使用 SwingUtilities.isEventDispatchThread()

谢谢

Note: I want a general rule how to know it without using the SwingUtilities.isEventDispatchThread()
Thanks

推荐答案


  1. 编号。

  2. 编号。

  3. 背景线程。

如果在EDT外部运行的代码调用GUI类中定义的方法,则该代码不会在EDT上运行,而是在调用线程中运行。

If code running outside the EDT calls a method defined in a GUI class, that code will not be run on the EDT but in the calling thread.

如果在EDT中运行的代码调用非GUI类中定义的代码,那么该代码将在EDT上运行。

If code running in the EDT calls code defined in a non-GUI class, that code will run on the EDT.

规则是,如果你'我没有创建一个不同的线程,你正在调用的方法将在调用代码运行的线程上运行 - 线程与wh不对应在类中定义方法。

The rule is that if you're not creating a different thread, the method you're calling will run on the thread the calling code is running from – threads do not correspond to what classes methods are defined in.

在SWT上调用的方法是事件监听器,而不是由您。 (如果你从EDT调用它们,它们可能仍然存在。)

Methods that will run on the EDT are event listeners, when they're called by Swing – not by you. (They still might be if you're calling them from the EDT though.)

此外, Runnable.run()<中的任何代码/ code>方法传递给 SwingUtilities.invokeLater() invokeAndWait() 也在EDT上运行。

Also, any code inside the Runnable.run() method passed to SwingUtilities.invokeLater() and invokeAndWait() is also run on the EDT.

您从EDT调用的任何常规方法都将在EDT上运行。

Any normal methods you call from the EDT will run on the EDT.

Thread <调用的任何代码/ code>你创建(无论是直接使用线程,还是 ExecutorService ,或 SwingWorker.doInBackground())在EDT上。你的程序的 main()方法在EDT上也是不是

Any code called from a Thread you create (whether using threads directly, or ExecutorService, or SwingWorker.doInBackground()) is not on the EDT. Your program's main() method is also not on the EDT.

这篇关于我怎么知道我是否在事件发送线程上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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