2012-10-04 49 views
4

我有一個搖動的Gui應用程序,它在串行熱敏打印機上打印票據。當我點擊啓動這個動作的按鈕時,我的GUI被凍結。我認爲這是因爲代碼是在EDT上執行的。我用jstack可以肯定的,但我不明白下面的結果:Java Swing:圖形用戶界面凍結 - jstack解釋

Full thread dump Java HotSpot(TM) Client VM (23.3-b01 mixed mode, sharing): 

"Thread-12" prio=6 tid=0x03012000 nid=0xd04 runnable [0x038ef000] 
java.lang.Thread.State: RUNNABLE 
    at gnu.io.RXTXPort.eventLoop(Native Method) 
    at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575) 

"Thread-6" prio=6 tid=0x0302c400 nid=0x1b0 waiting on condition [0x039ef000] 
java.lang.Thread.State: TIMED_WAITING (sleeping) 
    at java.lang.Thread.sleep(Native Method) 
    at posO2.Threads.ThreadHorloge.run(ThreadHorloge.java:46) 

"Thread-5" prio=6 tid=0x03511c00 nid=0x9e4 waiting on condition [0x0399f000] 
java.lang.Thread.State: TIMED_WAITING (sleeping) 
    at java.lang.Thread.sleep(Native Method) 
    at posO2.Threads.ThreadIgor.run(ThreadIgor.java:29) 

"Thread-7" prio=6 tid=0x034b9800 nid=0xb40 waiting on condition [0x0394f000] 
java.lang.Thread.State: TIMED_WAITING (sleeping) 
    at java.lang.Thread.sleep(Native Method) 
    at posO2.Threads.ThreadHorloge.run(ThreadHorloge.java:46) 
    at java.lang.Thread.run(Unknown Source) 

"Thread-4" prio=6 tid=0x0318ec00 nid=0xa08 waiting on condition [0x0323f000] 
java.lang.Thread.State: TIMED_WAITING (sleeping) 
    at java.lang.Thread.sleep(Native Method) 
    at posO2.Threads.ThreadMajProduits.run(ThreadMajProduits.java:49) 

"TimerQueue" daemon prio=6 tid=0x03036400 nid=0x9b8 waiting on condition [0x0344 
f000] 
java.lang.Thread.State: WAITING (parking) 
    at sun.misc.Unsafe.park(Native Method) 
    - parking to wait for <0x27e76778> (a java.util.concurrent.locks.Abstra 
ctQueuedSynchronizer$ConditionObject) 
    at java.util.concurrent.locks.LockSupport.park(Unknown Source) 
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject 
.await(Unknown Source) 
    at java.util.concurrent.DelayQueue.take(Unknown Source) 
    at javax.swing.TimerQueue.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 

"DestroyJavaVM" prio=6 tid=0x003c8400 nid=0xfe4 waiting on condition [0x00000000 
] 
java.lang.Thread.State: RUNNABLE 

"AWT-EventQueue-0" prio=6 tid=0x02b6e000 nid=0xcbc runnable [0x033fe000] 
java.lang.Thread.State: RUNNABLE 
    at java.net.SocketInputStream.socketRead0(Native Method) 
    at java.net.SocketInputStream.read(Unknown Source) 
    at java.net.SocketInputStream.read(Unknown Source) 
    at java.io.BufferedInputStream.fill(Unknown Source) 
    at java.io.BufferedInputStream.read(Unknown Source) 
    - locked <0x22f33f30> (a java.io.BufferedInputStream) 
    at java.io.DataInputStream.readByte(Unknown Source) 
    at org.hsqldb.result.Result.newResult(Unknown Source) 
    at org.hsqldb.ClientConnection.read(Unknown Source) 
    at org.hsqldb.ClientConnection.execute(Unknown Source) 
    - locked <0x22f32700> (a org.hsqldb.ClientConnection) 
    at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source) 
    at org.hsqldb.jdbc.JDBCStatement.executeQuery(Unknown Source) 
    - locked <0x22be23b0> (a org.hsqldb.jdbc.JDBCStatement) 
    at posO2.Printer.flash(Printer.java:1058) 
    at posO2.Printer.bigFlash(Printer.java:1111) 
    at posO2.Panels.Accueil$5.actionPerformed(Accueil.java:314) 
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
    at java.awt.Component.processMouseEvent(Unknown Source) 
    at javax.swing.JComponent.processMouseEvent(Unknown Source) 
    at java.awt.Component.processEvent(Unknown Source) 
    at java.awt.Container.processEvent(Unknown Source) 
    at java.awt.Component.dispatchEventImpl(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Window.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$200(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 

"AWT-Shutdown" prio=6 tid=0x02b81800 nid=0xefc in Object.wait() [0x033af000] 
java.lang.Thread.State: WAITING (on object monitor) 
    at java.lang.Object.wait(Native Method) 
    - waiting on <0x27c707c0> (a java.lang.Object) 
    at java.lang.Object.wait(Object.java:503) 
    at sun.awt.AWTAutoShutdown.run(Unknown Source) 
    - locked <0x27c707c0> (a java.lang.Object) 
    at java.lang.Thread.run(Unknown Source) 

"AWT-Windows" daemon prio=6 tid=0x03002800 nid=0xe58 runnable [0x0328f000] 
java.lang.Thread.State: RUNNABLE 
    at sun.awt.windows.WToolkit.eventLoop(Native Method) 
    at sun.awt.windows.WToolkit.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 

"Java2D Disposer" daemon prio=10 tid=0x03000400 nid=0x4d0 in Object.wait() [0x03 
1ef000] 
java.lang.Thread.State: WAITING (on object monitor) 
    at java.lang.Object.wait(Native Method) 
    - waiting on <0x27c2bf18> (a java.lang.ref.ReferenceQueue$Lock) 
    at java.lang.ref.ReferenceQueue.remove(Unknown Source) 
    - locked <0x27c2bf18> (a java.lang.ref.ReferenceQueue$Lock) 
    at java.lang.ref.ReferenceQueue.remove(Unknown Source) 
    at sun.java2d.Disposer.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 

"Service Thread" daemon prio=6 tid=0x02b63000 nid=0xb94 runnable [0x00000000] 
java.lang.Thread.State: RUNNABLE 

"C1 CompilerThread0" daemon prio=10 tid=0x02b54c00 nid=0x3f0 waiting on conditio 
n [0x00000000] 
java.lang.Thread.State: RUNNABLE 

"Attach Listener" daemon prio=10 tid=0x02b53400 nid=0x924 waiting on condition [ 
0x00000000] 
java.lang.Thread.State: RUNNABLE 

"Signal Dispatcher" daemon prio=10 tid=0x02b52000 nid=0xb88 runnable [0x00000000 
] 
java.lang.Thread.State: RUNNABLE 

"Finalizer" daemon prio=8 tid=0x02b43c00 nid=0xfc8 in Object.wait() [0x02dff000] 

java.lang.Thread.State: WAITING (on object monitor) 
    at java.lang.Object.wait(Native Method) 
    - waiting on <0x27c2c148> (a java.lang.ref.ReferenceQueue$Lock) 
    at java.lang.ref.ReferenceQueue.remove(Unknown Source) 
    - locked <0x27c2c148> (a java.lang.ref.ReferenceQueue$Lock) 
    at java.lang.ref.ReferenceQueue.remove(Unknown Source) 
    at java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source) 

"Reference Handler" daemon prio=10 tid=0x02b3f000 nid=0x488 in Object.wait() [0x 
02daf000] 
java.lang.Thread.State: WAITING (on object monitor) 
    at java.lang.Object.wait(Native Method) 
    - waiting on <0x27c2bdd8> (a java.lang.ref.Reference$Lock) 
    at java.lang.Object.wait(Object.java:503) 
    at java.lang.ref.Reference$ReferenceHandler.run(Unknown Source) 
    - locked <0x27c2bdd8> (a java.lang.ref.Reference$Lock) 

"VM Thread" prio=10 tid=0x02b3d400 nid=0xa6c runnable 

"VM Periodic Task Thread" prio=10 tid=0x02b76800 nid=0xf30 waiting on condition 


JNI global references: 426 

有人能向我解釋,在那裏我可以看到我的GUI是因爲第一個線程「主題-12」的凍結。

第二,「線程-6」和「線程-7」之間有什麼區別,只要我確信我只運行一次線程。

+0

打印呼叫是否被阻止?如果是阻塞呼叫,只需將其委託給不同的線程。 – Shark

回答

6

AWT-EventQueue-0是你的事件調度線程,它確實被阻止通過hsqldb 串口通過 RXTX 一個socket讀取。按照@Kumar的建議,您應該使用SwingWorker。例子可以發現herehere。我發現在profiler中研究這些例子是有幫助的。

Thread-6Thread-7作爲Threads.ThreadHorloge的實例在posO2中似乎屬於您的應用程序。關於線名:

每個thread都有一個名稱用於識別目的。多個線程可能具有相同的名稱。如果在創建線程時未指定名稱,則會爲其生成新名稱。

注意SwingWorkerExecutors通常包括文本池-N,其中ñ是序列號。

附錄:我EDT處於RUNNABLE狀態,所以從我粘貼代碼,你在哪裏弄清楚,它的堵塞;並且您在哪裏發現通過RXTX的讀數是阻塞原因

對不起,我的錯誤;上面的修正。 EDT不是BLOCKED中的Thread.STATE等待顯示器鎖定的意義;它在等待數據庫響應的意義上被阻塞,至少足夠長時間在發送-QUIT信號時可以在調用堆棧上看到。應該在EDT上安排串行和網絡操作。

+0

中構建好了,但是它表示我的EDT處於可運行狀態,所以從我粘貼的代碼中,您發現它的位置已被阻止,您在哪裏發現通過RXTX讀取是阻塞原因? – caRameL

5

一個非常基本的規則:

  • 保持UI線程上的用戶界面的工作,非UI線程上的非UI工作。

這樣我們就可以保持GUI 互動響應

  • 在Java 事件調度線程(EDT)是UI線程,在Java GUI應用程序main()方法沒有長期居住,所以調度GUI建設工作在事件調度隊列後退出然後它是處理GUI的EDT。

  • 要麼使用Thread做長的非UI處理工作,或使用SwingWorker,這在java做的UI和非UI工作之間的無縫同步提供......

3

這線程是真實從事件隊列

「AWT-EventQueue的-0」 PRIO = 6 TID = 0x02b6e000 NID = 0xcbc可運行 [0x033fe000] java.lang.Thread.State中啓動的一個:RUNNABLE

你可以告訴,因爲在這個胎面的堆棧跟蹤的盡頭,它是由EventDispatchThread開始:

在java.awt.EventDispatchThread.run(來源不明)

至於你關於「線程6和線程7有什麼區別」的問題,我沒有直接的答案,但我認爲大多數這些線程都是由你的分析器創建的(因此,重新看到它是旋轉起來的線程)。雖然這可能是錯誤的。我無法找到它正在引用的Threads.ThreadHorloge類中的任何內容。

+0

+1我認爲這些名字默認在'Thread' – trashgod