Java Swing : GUI frozen - jstack interpretation

前端 未结 3 1178
再見小時候
再見小時候 2020-12-21 01:42

I have a Gui application in swing that prints a ticket on a serial thermal printer. When i hit the button that launch this action, my GUI is frozen. I think that\'s because

相关标签:
3条回答
  • 2020-12-21 01:45

    One very basic rule:

    • Keep the UI work on the UI thread, and the Non-UI work on the Non-UI thread.

    This way we can keep the GUI interactive and responsive.

    • In Java Event Dispatcher Thread (EDT) is the UI thread, main() method in Java GUI application is not long lived, so after scheduling the work of GUI construction in the Event Dispatcher Queue it quits and then it's EDT that handles the GUI.

    • Either use Thread to do the Long Non-UI processing work, or use SwingWorker, this is provided in java to do a seamless synchronization between the UI and Non-UI work......

    0 讨论(0)
  • 2020-12-21 01:57

    AWT-EventQueue-0 is your event dispatch thread, and it is indeed blocked reading from a serial port via RXTX a socket via hsqldb. You should use SwingWorker, as suggested by @Kumar. Examples may be found here and here. I found it helpful to examine such examples in a profiler for study.

    Thread-6 and Thread-7 appear to belong to your application as instances of Threads.ThreadHorloge in posO2. Regarding thread names:

    Every thread has a name for identification purposes. More than one thread may have the same name. If a name is not specified when a thread is created, a new name is generated for it.

    Note that SwingWorker and Executors typically include the text pool-n, where n is a sequence number.

    Addendum: My EDT is in a RUNNABLE state, so from the code I pasted where do you figure out that it's blocked; and where do you find that the reading via RXTX is the blocking cause?

    Sorry, my mistake; corrected above. The EDT isn't BLOCKED in the Thread.STATE sense of waiting for a monitor lock; it's blocked in the sense that it's waiting for the database to respond, at least long enough to be seen atop the call stack when you send the -QUIT signal. Neither serial nor network operations should be scheduled on the EDT.

    0 讨论(0)
  • 2020-12-21 01:59

    This thread is the one that's started from the Event Queue

    "AWT-EventQueue-0" prio=6 tid=0x02b6e000 nid=0xcbc runnable [0x033fe000] java.lang.Thread.State: RUNNABLE

    You can tell because at the very end of the stack trace of this tread, it's started by the EventDispatchThread:

    at java.awt.EventDispatchThread.run(Unknown Source)

    As far as your question about "What's the difference between Thread 6 and Thread 7", I don't have a direct answer for you, but I'm thinking most of these threads were created by your profiler (as so what you're seeing is threads that it spun up). Could be wrong on that though. I can't find anything on the Threads.ThreadHorloge class it's referencing offhand.

    0 讨论(0)
提交回复
热议问题