事件调度线程的确切时间何时开始?

编程入门 行业动态 更新时间:2024-10-26 16:25:15
本文介绍了事件调度线程的确切时间何时开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

EDT确切何时开始?哪一行代码负责?

When exactly is the EDT started? What line of code is responsible of it?

我的猜测是"someSwingComponent.setVisible(true)"可以解决问题,但我不确定.

My guess is that "someSwingComponent.setVisible(true)" does the trick, but I'm not sure.

谢谢!

推荐答案

问:EDT确切何时开始?哪一行代码负责[f]?

Swing的内部工作方式是特定于JVM的.不同的JVM基于不同的条件启动事件调度线程(EDT).一般而言:

Q: When exactly is the EDT started? What line of code is responsible [f]of it?

The inner workings of Swing are JVM-specific. Different JVMs start the Event Dispatch Thread (EDT) based on differing criteria. In general though:

EDT在收到其第一个 AWTEvent 时启动.

下面的堆栈跟踪重申了这一点.以下面的 main 方法为例.

The stack traces below reaffirm this point. Take for example the following main method.

public static void main(String[] args) { JFrame frame = new JFrame(); frame.setVisible(true); }

在上面的示例中,负责启动EDT的代码行为 frame.setVisible(true);

In the example above, the line of code responsible for starting the EDT is frame.setVisible(true);

上面的 main 方法是在两个不同的JVM上执行的.在 EventQueue.initDispatchThread 处放置了一个断点.当达到断点时,将记录以下堆栈跟踪.

The above main method was executed on two different JVMs. A breakpoint was placed at EventQueue.initDispatchThread. When the breakpoint was hit, the following stack traces were noted.

在 AWT-AppKit 线程上使用Mac的JDK:

Using the Mac's JDK on the AWT-AppKit thread:

EventQueue.initDispatchThread() line: 906 EventQueue.wakeup(boolean) line: 1109 NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method] NativeMethodAccessorImpl.invoke(Object, Object[]) line: 39 DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 25 Method.invoke(Object, Object...) line: 597 SunToolkit.wakeupEventQueue(EventQueue, boolean) line: 348 PostEventQueue.postEvent(AWTEvent) line: 2137 SunToolkit.postEvent(AppContext, AWTEvent) line: 583 SunToolkit.executeOnEventHandlerThread(PeerEvent) line: 654 SunToolkit.executeOnEventHandlerThread(Object, Runnable) line: 631 EventFactoryProxy.windowMoved(CWindow) line: 89

在主线程上使用Oracle的Windows JDK:

Using Oracle's JDK for Windows on the main thread:

java.awt.EventQueue.initDispatchThread() line: 861 java.awt.EventQueue.postEventPrivate(java.awt.AWTEvent) line: 199 java.awt.EventQueue.postEvent(java.awt.AWTEvent) line: 180 javax.swing.RepaintManager.scheduleProcessingRunnable(sun.awt.AppContext) line: 1369 javax.swing.RepaintManager.nativeAddDirtyRegion(sun.awt.AppContext, java.awt.Container, int, int, int, int) line: 548 javax.swing.SwingPaintEventDispatcher.createPaintEvent(java.awt.Component, int, int, int, int) line: 45 sun.awt.windows.WFramePeer(sun.awt.windows.WComponentPeer).postPaintIfNecessary(int, int, int, int) line: 741 sun.awt.windows.WFramePeer(sun.awt.windows.WComponentPeer).handlePaint(int, int, int, int) line: 736 sun.java2d.d3d.D3DScreenUpdateManager.repaintPeerTarget(sun.awt.windows.WComponentPeer) line: 274 sun.java2d.d3d.D3DScreenUpdateManager.createScreenSurface(sun.awt.Win32GraphicsConfig, sun.awt.windows.WComponentPeer, int, boolean) line: 175 ... sun.awt.windows.WToolkit.createFrame(java.awt.Frame) line: 383 javax.swing.JFrame(java.awt.Frame).addNotify() line: 460 javax.swing.JFrame(java.awt.Window).show() line: 859 javax.swing.JFrame(java.awt.Component).show(boolean) line: 1584 javax.swing.JFrame(java.awt.Component).setVisible(boolean) line: 1536 javax.swing.JFrame(java.awt.Window).setVisible(boolean) line: 842 Example.main(java.lang.String[]) line: 113

在Mac上,调用 PostEventQueue.postEvent(AWTEvent).类似地,在Windows上,也会调用 java.awt.EventQueue.postEvent(java.awt.AWTEvent).两者最终都调用 EventQueue.initDispatchThread .

On the Mac, a call to PostEventQueue.postEvent(AWTEvent) is made. Similarly on Windows, a call to java.awt.EventQueue.postEvent(java.awt.AWTEvent) is made. Both eventually call EventQueue.initDispatchThread.

作为另一个示例,请考虑以下 main 方法:

As another example, consider the following main method:

public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { System.out.println("Start!"); } }); }

在主线程上使用Mac的JDK:

Using the Mac's JDK on the main thread:

EventQueue.initDispatchThread() line: 906 [local variables unavailable] EventQueue.postEventPrivate(AWTEvent) line: 227 EventQueue.postEvent(AWTEvent) line: 208 EventQueue.invokeLater(Runnable) line: 1048 SwingUtilities.invokeLater(Runnable) line: 1267 Example.main(String[]) line: 31

在主线程上使用Oracle的Windows JDK:

Using Oracle's JDK for Windows on the main thread:

java.awt.EventQueue.initDispatchThread() line: 861 java.awt.EventQueue.postEventPrivate(java.awt.AWTEvent) line: 199 java.awt.EventQueue.postEvent(java.awt.AWTEvent) line: 180 java.awt.EventQueue.invokeLater(java.lang.Runnable) line: 999 javax.swing.SwingUtilities.invokeLater(java.lang.Runnable) line: 1267

对 SwingUtilties.invokeLater 的调用负责启动EDT.再次在这里,调用 EventQueue.postEvent(AWTEvent).

The call to SwingUtilties.invokeLater is responsible for starting the EDT. Here again, calls to EventQueue.postEvent(AWTEvent) are made.

不仅对 someSwingComponent.setVisible(true)的任何调用都会启动EDT.例如,执行以下 main 方法不会创建 AWT-Event-Queue-0 线程:

Not just any call to someSwingComponent.setVisible(true) will start the EDT. For example, executing the following main method does not create the AWT-Event-Queue-0 thread:

public static void main(String[] args) { JLabel label = new JLabel(); label.setVisible(true); }

资源

当然,在线上有许多有关EDT的资源.

Resources

Of course, there are many resources online about the EDT.

    Java的
  • 事件调度线程" 教程的课程:Swing中的并发"
  • 线程和摆动" 来自Sun Developer Network
  • "Swing Threads" (来自Java词汇表,网址为mindprod)
  • 事件分配线程如何工作?" 堆栈溢出
  • "The Event Dispatch Thread" from The Java Tutorials's "Lesson: Concurrency in Swing"
  • "Threads and Swing" from the Sun Developer Network
  • "Swing Threads" from the Java Glossary at mindprod
  • "How does the event dispatch thread work?" on Stack Overflow

更多推荐

事件调度线程的确切时间何时开始?

本文发布于:2023-11-27 05:40:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636816.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   确切   事件   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!