为什么Web应用程序挂起SwingUtilities.isEventDispatchThread()

时间:2012-05-29 12:48:05

标签: java multithreading swing x11 hang

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

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

我们在linux 6上的java 6,tomcat 7上。

更新:

我们删除了对isEventDispatchThread()的调用,这样我的问题就解决了,但我仍然对这个问题的答案感兴趣。

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

java.awt.Toolkit.getDefaultToolkit似乎导致锁定,因为它已同步。所以Yura正在朝着正确的方向发展。

所以问题仍然存在:为什么不完整或者为什么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)

1 个答案:

答案 0 :(得分: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

希望这有帮助