简体   繁体   English

从Swing中的事件调度线程之外的另一个线程中泵出事件

[英]Pump events from another thread than the Event Dispatching Thread in Swing

In my application, some process that is started in AWT's Event Dispatching Thread (EDT) may be interrupted under particular circumstances. 在我的应用程序中,在某些情况下,可能会中断AWT的事件调度线程(EDT)中启动的某些过程。 Then it waits for input from the user. 然后,它等待来自用户的输入。 Alas, as the process is located in the EDT, the whole application freezes and the user is unable to relaunch the process, thus creating a deadlock. the,由于该进程位于EDT中,因此整个应用程序将冻结,并且用户无法重新启动该进程,从而造成死锁。 Is there any way to interrupt the EDT and lauch a new event pump from another thread ? 有没有办法中断EDT并从另一个线程启动新的事件泵? Then the user would be able to interact with the UI. 然后,用户将能够与UI进行交互。

What I am trying to do looks more or less like opening a modal dialog, except that I don't want a dialog because my component is complex. 我尝试执行的操作或多或少类似于打开模式对话框,但由于我的组件很复杂,我不希望使用对话框。 I'd rather display it right inside my root pane. 我宁愿将其显示在我的根窗格中。 So I had a look at how it's done in java.awt.Dialog, and had a pretty good understanding of it, but the most important classes that are used (EventDispatchThread, SequencedEvent, ...) are protected and therefore inaccessible for me. 因此,我看了一下如何在java.awt.Dialog中完成此操作,并对它有了很好的理解,但是所使用的最重要的类(EventDispatchThread,SequencedEvent等)受到了保护,因此我无法访问。


Thank you all for your answers. 谢谢大家的答案。

I'll be more specific. 我会更具体。 I am actually working on an application which embeds a homegrown scripting language. 我实际上正在开发一个嵌入本地脚本语言的应用程序。 I am developing a debugger for this language. 我正在为此语言开发调试器。 The debugger (as all debuggers) would stop the script execution whenever a breakpoint is met. 每当遇到断点时,调试器(与所有调试器一样)将停止脚本执行。 The scripted processes can be triggered from many places (including from the EDT) in the code so taking the the process out of the EDT is not an option. 脚本化的过程可以从代码中的许多地方(包括EDT)触发,因此将过程从EDT中移除是不可取的。 I'd like the debugger UI to be embedded in the application (in a side pane to be precise). 我希望将调试器UI嵌入到应用程序中(准确地说是在侧窗格中)。 So when a breakpoint is met I would need the current thread (possibly the EDT, not to say mainly) to be interrupted and at least the debugger's UI should still be responsive. 因此,当遇到断点时,我将需要中断当前线程(可能是EDT,主要是说EDT),至少调试器的UI仍应响应。 Also I am developing atop of the JDK 1.4 so no way to use JDK7 alas. 另外,我正在JDK 1.4之上开发,因此无法使用JDK7。

What I am currently doing is opening a JDialog with the debugger embedded. 我目前正在做的是打开一个嵌入调试器的JDialog。 It all works fine but as I said I am not fully satisfied by this solution because I would really want my debugger to be embedded right in my main window. 一切正常,但是正如我说的那样,我对这种解决方案不完全满意,因为我真的希望将调试器嵌入到主窗口中。

If you are using JDK7, you can use the SecondaryLoop interface. 如果使用的是JDK7,则可以使用SecondaryLoop接口。 An instance of this interface can be created through the EventQueue.createSecondaryLoop method 可以通过EventQueue.createSecondaryLoop方法创建此接口的实例。

There was an informative blog post with an example but the server seems to be offline for the moment. 一个内容丰富的博客文章,其中有一个示例,但是服务器暂时处于离线状态。

A small edit as I am still not fully sure I understand your question. 稍作修改,因为我仍然不确定我是否理解您的问题。

If you want to wait for user input from a worker thread, you can use the SwingUtilities.invokeAndWait method and use a blocking method (eg show a JOptionPane to retrieve user input). 如果要等待来自工作线程的用户输入,则可以使用SwingUtilities.invokeAndWait方法并使用阻塞方法(例如,显示JOptionPane来检索用户输入)。 Due to the invokeAndWait your worker thread will be stopped until the Runnable on the EDT is processed. 由于invokeAndWait您的工作线程将被停止,直到处理EDT上的Runnable If you use a blocking method on the EDT to retrieve the user input that Runnable will only be finished when the user has provided his/her input 如果您在EDT上使用阻止方法来检索用户输入,则Runnable仅在用户提供其输入后才能完成。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM