Multithread Swing programming [Java]

There are few situations when a Swing applications need to "sleep" or have to perform an action that takes some time, for example call a system command and then wait for the execution to finish. If you try to sleep or block the Swing thread using Thread.spleep() method, the window/dialog seems to block until the thread becomes "unsleeped".

The solution might be to divide application logic and graphical user interface into separate threads so that when the application logic element locks, the other runs independent.

The controller (application logics) is linked with GUI trough so called GUIDriver. GUIDriver is composed of Swing components (private fields: frames, dialogs etc.) and GUIMessenger (public static field) that acts as the mailbox. GUIDriver also exposes a simple inerface for controller to GUI communication so that the implementation of GUI might be hidden from the controller.

The controller must implement the idle method (as run() method of the controller thread) that listens for new messages from GUI. This way, we do not need to pass the reference of the controller to GUI (this would create a kind of a deadlock).

Simplified activity diagram for a car park

IDLE diagram

Powered by PepisCMS