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.
谢谢!
Swing的内部工作方式是特定于JVM的.不同的JVM基于不同的条件启动事件调度线程(EDT).一般而言:
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的资源.
Of course, there are many resources online about the EDT.
这篇关于事件调度线程的确切时间何时开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!