Basil Bourque
Basil Bourque

Reputation: 339679

Verify code is running on user-interface thread in Vaadin 7 app

Is there some way to verify that code is executing on the user’s user interface thread (event loop thread)?

This question is the Vaadin equivalent of this question, Swing verify code on Event Dispatch Thread at runtime.

I know how to call UI::access from a background thread to schedule a Runnable to be run on the user-interface thread. My question is how to double-check that some executing code is indeed running on the user-interface thread.

I filed a feature request for this.

Upvotes: 0

Views: 188

Answers (2)

Leif Åstrand
Leif Åstrand

Reputation: 8001

The framework is already full of asserts in various code paths that should only be run on the thread that has locked the UI state. The most important being whenever shared state is accessed.

To benefit from this checking, just make sure your server is run with assertion checking enabled, i.e. by starting it with the -ea JVM parameter.

Upvotes: 1

mstahv
mstahv

Reputation: 1934

UI.getCurrent()

If UI.getCurrent() returns an instance you are (most probably) either:

  • On a thread started by UI interaction
  • On a thread that is already initiated with UI::access

To quote the Vaadin 7.3.9 doc:

… In other cases, (e.g. from background threads), the current UI is not automatically defined.

Example Code

boolean uiOrUiAccessManagedThread = UI.getCurrent() != null;

Upvotes: 1

Related Questions