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
One very basic rule:
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......
AWT-EventQueue-0
is your event dispatch thread, and it is indeed blocked reading from a serial port via a socket via RXTX
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.
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.