当dumpStackTrace的第一个元素位于java.awt.EventDispatchThread.run时,为什么isEventDispatchThread()返回false?
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
doStuff();
}
});
public void doStuff(){
Thread.dumpStack();
System.out.print(" [addToParents] "
+ ((SwingUtilities.isEventDispatchThread()
? "out of EDT" : " in EDT ")));
}
产量:
[addToParents] out of EDT
java.lang.Exception: Stack trace
at java.lang.Thread.dumpStack(Thread.java:1342)
at pt.up.fe.dceg.neptus.gui.MissionBrowser.doStuff(MissionBrowser.java:899)
at pt.up.fe.dceg.neptus.gui.MissionBrowser.access$6(MissionBrowser.java:883)
at pt.up.fe.dceg.neptus.gui.MissionBrowser$3.run(MissionBrowser.java:1221)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
这是简化的代码(不是MWS)。我怀疑这是一个我没有理解的概念,如果没有MWS,请告诉我。
最佳答案
不,不是。在三元运算符的表达式中:
(SwingUtilities.isEventDispatchThread() ? "out of EDT" : " in EDT ")))
SwingUtilities.isEventDispatchThread()
返回true
,这就是为什么打印"Out of EDT"
的原因。对于表达式
condition ? value1 : value2
:如果condition
为true,则结果为value1
,否则结果为value2
。