Thread synchronization in java pdf


The event dispatching thread is the only valid thread to update the visual state of visible user interface components. This means GUI is a shared thread synchronization in java pdf structure and some synchronization is needed to ensure that only one thread accesses it at a time.

GUI components and these methods are visible to all application threads, likewise in other GUI frameworks, only a single, Event Dispatching thread has the right to execute these methods. That is, likewise in other GUI frameworks, the Event Dispatching Thread spends its life pumping messages: it maintains a message queue of actions to be performed over GUI. These requests are submitted to the queue by system and any application thread. EDT consumes them one after another and responds by updating the GUI components.

The messages may be well-known actions or involve callbacks, the references to user-methods that must be executed by means of EDT. The important requirement imposed on all messages is that they must be executed quickly for the GUI to stay responsive. Otherwise, the message loop is blocked and GUI freezing is experienced. There are various solutions for submitting code to the EDT and performing lengthy tasks without blocking the loop. GUI components support the lists of callbacks, called Listeners, which are typically populated when the components are created. EDT must be as quick as possible to prevent freezing. Java 6 is part of standard Swing distribution.

Spawning a worker thread, EDT proceeds handling current message without waiting the worker to complete. Often, this is not desirable. The vicious cycle is broken through EDT entering a new message loop, which dispatches the messages as per normal until “modal dialog is over” arrives and normal message processing resumes from the blocked position in the component action. Swing message loop pumping to provide the “synchronous” execution mechanism for arbitrary user tasks, which proceeds only after the worker completes the task.