简体   繁体   English

我可以运行两个EDT吗?

[英]Can I run two EDTs?

This is mostly a theoretical question. 这主要是一个理论问题。 The example below is what made me think of it, but it may not be the best example. 下面的例子让我想到了它,但它可能不是最好的例子。 Please assume that the reason's below are concrete, and can't for the moment be worked around. 请假设下面的原因是具体的,暂时不能解决。

The Program I have been running has an optional Debug frame that is created on the program startup, and it made visible by the user pressing buttons/keyboard shortcut. 我运行的程序有一个可选的Debug框架,它是在程序启动时创建的,用户按下按钮/键盘快捷键可以看到它。

My issue is that as I have lengthy processes on the EDT, if it is hanging for some reason or fails I'd like to see straight away, and not wait for that thread to end running, to update the Debug Log Frame. 我的问题是,由于我在EDT上有冗长的进程,如果由于某种原因而挂起或失败,我想立即看到,而不是等待该线程结束运行,以更新调试日志帧。

My solution would be to have two separate EDT for two separate GUIs that are updated by a separate thread. 我的解决方案是为两个单独的GUI提供两个单独的EDT,这些GUI由一个单独的线程更新。

Is this possible, or am I not able to do so? 这可能,或者我不能这样做? I haven't found any online resourcethat would show me how to. 我还没有找到任何可以告诉我如何使用的在线资源。 I know that EDTs should be single threaded, but if I keep the threads disentangled, can I have two? 我知道EDT应该是单线程的,但如果我保持线程解开,我可以有两个吗? Please? 请?

The answer is simple: No you cannot have 2 EDTs , that is not possible. 答案很简单:不,你不能有2个EDT ,这是不可能的。 But you are not stuck with a frozen GUI, you have some options available. 但是你没有使用冻结的GUI,你有一些选择。

First and foremost, two important rules: 首先,有两条重要规则:

  1. Never do lengthy calculations in the EDT. 永远不要在EDT中进行冗长的计算。 Ever. 永远。
  2. Never manipulate Swing components from outside the EDT. 切勿操纵EDT外部的Swing组件。 Ever. 永远。

Breaking the first rule will result in your GUI being frozen and no events of any sort being processed during that time. 断开第一条规则将导致GUI被冻结,并且在此期间不会处理任何类型的事件。 This includes updates to the GUI you want to do during the calculations which will not appear until after the calculations are done. 这包括在计算过程中要进行的GUI更新,直到计算完成后才会出现。

The latter is often ignored which will go by unnoticed most of the time, but it can - and will - bite you in the ass and then most of the time it is a huge pita to go back and fix it. 后者经常被忽略,大部分时间都会被忽视,但它可以 - 并且会 - 咬你的屁股,然后大部分时间回到修复它是一个巨大的皮塔。 So do it the correct way from the start. 所以从一开始就采用正确的方法。 What can happen? 怎么会发生什么? Components can suddenly display in a broken state, they may appear white, or the whole application can freeze because there is a deadlock between the EDT and your other threads (been there, done that). 组件可能会突然显示为处于损坏状态,它们可能显示为白色,或者整个应用程序可能会冻结,因为EDT与其他线程之间存在死锁(已执行此操作)。 Adhere to the Oracle Swing Threading Policy ! 坚持Oracle Swing线程策略


So, how to avoid doing lengthy calculations on the EDT after for example the user pressed a button? 那么,例如用户按下按钮后,如何避免在EDT上进行冗长的计算? Options: 选项:

  • use a SwingWorker . 使用SwingWorker Advantage: Has a done() method you can use which is automatically executed in the EDT once the worker is done. 优点:您可以使用done()方法,一旦工作完成,该方法将在EDT中自动执行。 Also implements the Future interface and can therefore be used to return a result. 还实现了Future接口,因此可用于返回结果。
  • you can just create your own Runnable and do the calculations in there. 你可以创建自己的Runnable并在那里进行计算。
  • use any other way Java provides for parallel execution. 使用Java为并行执行提供的任何其他方式。

Ok, and how to avoid ever manipulating GUI from outside the EDT? 好吧,以及如何避免不断从外面EDT操纵GUI?

  • call SwingUtilities.invokeLater() and execute Swing manipulations in there. 调用SwingUtilities.invokeLater()并在那里执行Swing操作。
  • use the SwingWorkers done() method as described above. 使用如上所述的SwingWorkers done()方法。

If you follow these two rules, your Swing GUI will be in a much better shape and you can focus more on your application development rather than Swing issues. 如果您遵循这两个规则,您的Swing GUI将处于更好的状态,您可以更专注于应用程序开发而不是Swing问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以在两个不同的端口上运行weblogic吗? - Can I run weblogic on two different port? 如何运行具有两个独立作业的批处理项目? - How can I run a batch project with two independent jobs? 我可以在同一台机器上运行两个tomcat实例吗? - Can I run two tomcat instances on the same machine? 我如何制作可以同时运行这两个课程的第三堂课? - How can I make a third class that could run these two classes? 如果我在同一个类上同步了两个方法,它们可以同时运行吗? - If I synchronized two methods on the same class, can they run simultaneously? 如何使用批处理文件运行两个Java应用程序? - How can i run two java applications using a batch file? 如何同时运行两个不同的主类? - How can I run two different main class at the same time? 如何运行两个Tomcat服务器v7.0 - How can I run two Tomcat server v7.0 为什么我的代码不能同时运行两个面板? 当我运行它时它不会显示任何内容,但它会编译 - Why can't my code run two panels at same time? it doesnt show anything when I run it, but it compiles 我可以在tomcat的不同上下文路径中运行两次相同战争的副本吗? - can i run two copies of the same war in different context paths on tomcat?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM