gpt4 book ai didi

java - SunToolkit.awtLock : does code that takes such a lock needs to be called on the EDT

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:57:06 27 4
gpt4 key购买 nike

我正在调查死锁并在线程转储中看到以下内容

at sun.awt.SunToolkit.awtLock(SunToolkit.java:229)
at sun.awt.X11.XRobotPeer.setup(Native Method)
- locked <0x00000000a633fbd0> (a java.lang.Class for sun.awt.X11.XRobotPeer)
at sun.awt.X11.XRobotPeer.<init>(XRobotPeer.java:24)
at sun.awt.X11.XToolkit.createRobot(XToolkit.java:683)
at java.awt.Robot.init(Robot.java:119)
at java.awt.Robot.<init>(Robot.java:77)

这是调用Robot robot = new Robot();引起的

这个调用需要一个锁 ( SunToolkit.awtLock ),我想知道还有谁在使用那个锁,如果我移动那个 new Robot() 会不会更好调用美国东部时间。顾名思义,它由单线程的 AWT 使用。如果 EDT 上的某些东西也占用了这个锁(例如 Swing 组件),那么当我创建 Robot 时,我遇到死锁的机会就会增加。关闭 EDT。

另一方面,如 this question 中所述, 很多 Robot方法旨在在 EDT 上调用时抛出异常。如果您最好创建 Robot,这会让它变得很烦人。 EDT 上的实例。

Toolkit.getDefaultToolkit().getScreenResolution() 也存在同样的问题所以不需要只关注 Robot类:

at sun.awt.SunToolkit.awtLock(SunToolkit.java:229)
at sun.awt.X11.XToolkit.getScreenResolution(XToolkit.java:999)

所以我想弄清楚的是:

  • 谁是这把锁的利益相关方?
  • 该锁是否可能只是为了使 Swing/AWT 成为多线程(或至少更加线程安全)而引入的,但是推荐的方法是避免在 EDT 之后的另一个线程上获取该锁吗?
  • 是否有我可以查阅的任何官方 Oracle/Sun 文档(例如 Concurrency in Swing 指南)?我的 Google 技能在这方面让我失望了。

最佳答案

请在此处查看 SunToolkit.java 的源代码:http://www.docjar.com/html/api/sun/awt/SunToolkit.java.htmlAWT_LOCK 的用途在第 208 行进行了解释。

以下是页面消失时的摘录:

/** 
* The AWT lock is typically only used on Unix platforms to synchronize
* access to Xlib, OpenGL, etc. However, these methods are implemented
* in SunToolkit so that they can be called from shared code (e.g.
* from the OGL pipeline) or from the X11 pipeline regardless of whether
* XToolkit or MToolkit is currently in use. There are native macros
* (such as AWT_LOCK) defined in awt.h, so if the implementation of these
* methods is changed, make sure it is compatible with the native macros.
*
* Note: The following methods (awtLock(), awtUnlock(), etc) should be
* used in place of:
* synchronized (getAWTLock()) {
* ...
* }
*
* By factoring these methods out specially, we are able to change the
* implementation of these methods (e.g. use more advanced locking
* mechanisms) without impacting calling code.
*
* Sample usage:
* private void doStuffWithXlib() {
* assert !SunToolkit.isAWTLockHeldByCurrentThread();
* SunToolkit.awtLock();
* try {
* ...
* XlibWrapper.XDoStuff();
* } finally {
* SunToolkit.awtUnlock();
* }
* }
*/

关于java - SunToolkit.awtLock : does code that takes such a lock needs to be called on the EDT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16035933/

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