Search code examples
javaconcurrencyvaadin8

using the same UI instance inside access method


I have a class that implements Runnable and provides additional field to store current custom UI instance (extends UI class) for thread.

Inside run method it invokes

@Override
public void run() {
    ui.access(() -> ui.getPoolingManager().unregisterPollRequest(this));
}

Where ui is the field of the current thread set upon thread instantiation.

The question - is there some possible concurrency issues when using the same ui instance to invoke access and getPoolingManager (custom method) inside labmda?


Solution

  • Unless the PoolingManager itself uses ThreadLocals or similar I don't think so. You hold the lock on the UI instance within the access method so accessing other fields/methods is unproblematic.

    From the documentation: "The given runnable is executed while holding the session lock to ensure exclusive access to this UI. If the session is not locked, the lock will be acquired and the runnable is run right away. If the session is currently locked, the runnable will be run before that lock is released." and "Please note that the runnable might be invoked on a different thread or later on the current thread, which means that custom thread locals might not have the expected values when the command is executed."