gpt4 book ai didi

java - isEventDispatchThread 超出 EDT 但从 EventDispatchThread.run 运行

转载 作者:行者123 更新时间:2023-11-29 03:28:37 25 4
gpt4 key购买 nike

当 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” 的原因。

对于表达式 条件 ? value1 : value2 :如果 condition 为真,则结果为 value1 否则结果为 value2

关于java - isEventDispatchThread 超出 EDT 但从 EventDispatchThread.run 运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19666695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com