1

我有一段代码需要在 Swing 应用程序和 web 应用程序中运行。但它应该在每个环境中做些稍微不同的事情。因此我们检查SwingUtilities.isEventDispatchThread()

在正常工作一段时间后,这突然停止工作。它只是挂在那个电话上,永远不会回来。知道为什么会这样吗?

我们在 linux 机器上使用 java 6、tomcat 7。

更新:

我们删除了对 isEventDispatchThread() 的调用,所以我的直接问题得到了解决,但我仍然对为什么会发生这种情况的答案感兴趣。

我们又调试了一点,得到了一个线程转储,并为一个线程找到了下面的东西。它显示许多线程正在等待它。所以这就解释了悬挂。

java.awt.Toolkit.getDefaultToolkit似乎导致锁定,因为它是同步的。所以尤拉暗示了正确的方向。

所以问题仍然存在:为什么它没有完成或将问题转向为什么会sun.awt.X11GraphicsEnvironment.initDisplay(X11GraphicsEnvironment.java:-1)挂起?

机器上正在运行X11....

"http-bio-8081-exec-10@10344" daemon prio=5 tid=0x1ae nid=NA runnable
  java.lang.Thread.State: RUNNABLE
  blocks http-bio-8081-exec-13@10809
  blocks http-bio-8081-exec-12@10786
  blocks http-bio-8081-exec-11@10343
  blocks http-bio-8081-exec-9@10345
  blocks http-bio-8081-exec-7@10346
  blocks http-bio-8081-exec-6@10347
  blocks http-bio-8081-exec-4@10349
  blocks http-bio-8081-exec-3@10352
  blocks http-bio-8081-exec-2@10353
  blocks http-bio-8081-exec-1@10354
   at sun.awt.X11GraphicsEnvironment.initDisplay(X11GraphicsEnvironment.java:-1)
   at sun.awt.X11GraphicsEnvironment.access$100(X11GraphicsEnvironment.java:52)
   at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:155)
   at java.security.AccessController.doPrivileged(AccessController.java:-1)
   at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:131)
   at java.lang.Class.forName0(Class.java:-1)
   at java.lang.Class.forName(Class.java:169)
   at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:68)
   - locked <0x2507> (a java.lang.Class)
   at sun.awt.X11.XToolkit.<clinit>(XToolkit.java:89)
   at java.lang.Class.forName0(Class.java:-1)
   at java.lang.Class.forName(Class.java:169)
   at java.awt.Toolkit$2.run(Toolkit.java:834)
   at java.security.AccessController.doPrivileged(AccessController.java:-1)
   at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:826)
   - locked <0xf9d> (a java.lang.Class)
   at java.awt.Toolkit.getEventQueue(Toolkit.java:1689)
   at java.awt.EventQueue.isDispatchThread(EventQueue.java:857)
   at javax.swing.SwingUtilities.isEventDispatchThread(SwingUtilities.java:1339)
   at xxxxxxxxxA.setCurrentUser(xxxxxxxxxA.java:196)
   at xxxxxxxxxB.setCurrentUser(xxxxxxxxxB.java:41)
   at xxxxxxxxxC.UserThreadLocalHandler.afterCompletion(xxxxxxxxxC.java:96)
   at org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletion(DispatcherServlet.java:1108)
   at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:830)
   at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
   at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
   at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:574)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.springframework.web.filter.ShallowEtagHeaderFilter.doFilterInternal(ShallowEtagHeaderFilter.java:58)
   at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
   at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
   at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:162)
   at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
   at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
   at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
   at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
   at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
   at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
   - locked <0x2a42> (a org.apache.tomcat.util.net.SocketWrapper)
   at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
   at java.lang.Thread.run(Thread.java:662)
4

1 回答 1

1

在后台,SwingUtilities.isEventDispatchThread()调用Toolkit.getEventQueue()方法,该方法具有以下来源:

/* Accessor method for use by AWT package routines. */
static EventQueue getEventQueue() {
    return getDefaultToolkit().getSystemEventQueueImpl();
}

Toolkit.getDefaultToolkit被声明为synchronized方法 - 也许就是这种情况。另外,请参考此方法的JavaDoc:http: //docs.oracle.com/javase/6/docs/api/java/awt/Toolkit.html#getDefaultToolkit%28%29

希望这可以帮助

于 2012-05-29T13:08:28.533 回答