8

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

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.awtLocknew Robot()(顾名思义,它由单线程的 AWT 使用。如果 EDT 上的某些东西也使用了这个锁(例如 Swing 组件),那么当我Robot在 EDT 之外创建它时,我遇到死锁的机会就会增加。

另一方面,正如在这个问题中所讨论的那样,许多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 文档(类似于 Swing 中的并发指南)可供我查阅?我的谷歌技能让我失望了。
4

1 回答 1

1

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

这是页面消失时的摘录:

/** 
 * 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();
 *         }   
 *     }   
 */
于 2013-05-07T09:26:19.930 回答