0

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.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154

2 Answers2

1

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;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
mstahv
  • 1,784
  • 9
  • 7
  • Thread started by UI interaction does not by itself synchronize anything. You'd also want to check ui.getSession().hasLock(). – Leif Åstrand Jan 30 '15 at 14:53
1

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.

Leif Åstrand
  • 7,820
  • 13
  • 19