Multi-threading to boost responsiveness

To perform basic operations on models and views in LuciadLightspeed, you must make use of threading and locking. LuciadLightspeed provides the TLcdLockUtil class in its API to assist with these operations, and defines a set of multi-threading rules.

For an overview of those fundamental LuciadLightspeed threading and locking rules, see Threading and locking in LuciadLightspeed.

To deal with possible performance issues in your application, however, LuciadLightspeed allows you to go beyond the basic rules. This article describes scenarios that deviate from LuciadLightspeed threading fundamentals to improve your application’s responsiveness. In this approach, you do not have to use the EDT at all times to apply model and view changes. You can offload the EDT by:

  • Updating a model off the EDT to handle live data updates and similar scenarios

  • Preparing model updates off the EDT, and swapping the updates into the model on the EDT.

  • Painting layers asynchronously in GXY views

Offloading the EDT provides more information about these three scenarios.

If you are using offscreen views, you do not need to use the EDT either. For more information about this specific case, see Multi-threading to support offscreen views.

Offloading the EDT

You can apply various strategies to reduce the workload for the EDT and divert some of the work to a different thread.

Updating a model off the EDT

If your application model undergoes substantial or frequent changes, performing these changes on the EDT could affect UI response time: the view is slow to respond while all these changes are processed on the EDT. This might happen if you are updating airplane tracks from a live feed over a network connection, for example.

To prevent such a negative impact on the user experience, LuciadLightspeed allows you to perform model updates off the EDT. You can use any thread on condition that:

  • You are using a Lightspeed view, not a GXY view

  • You do not have other GUI elements that depend on the model, like a table view

  • Your application does not allow users to edit the data using an ILspEditor

  • You do not have selection listeners that perform GUI operations

  • You are not using the TLcdSimulator of the Real-time Engine component

  • You are not building your application in Lucy, which offers all the elements of the previously listed conditions

Keep in mind that you still need to use a single thread for your operations, and that you still need to lock the model.

If any of the conditions in the list above are not met, see Preparing model updates off the EDT for a possible alternative.

Preparing model updates off the EDT

If you want to perform model updates off the EDT, but cannot meet the conditions listed in Updating a model off the EDT, you can use a prepare-and-swap pattern to offload most of the work. In this approach, you:

  1. Prepare the model updates on any thread. You are not changing the model itself yet, so model write locks are not necessary at this point. You may still need to take read locks, though. For example, you can read data from disk or from a socket, parse, convert or pre-process data.

  2. Swap the model updates to the EDT, and apply them to the model in batch, with a write lock.

You can use SwingUtilities.invokeLater to schedule the swap on the EDT.

Asynchronous layer painting in GXY views

In GXY views, you can use asynchronous layer wrappers to paint the views asynchronously. This is useful if your GXY views have layers that take a long time to paint.

For threading guidelines with asynchronously painted layers in GXY views, see the Asynchronous painting documentation.