PL-1758 JavaDoc for UIAccessor and IllegalConcurrentAccessException

This commit is contained in:
Yuriy Artamonov 2016-05-30 14:53:50 +04:00
parent c5645ab63c
commit ff4965290e
5 changed files with 38 additions and 1 deletions

View File

@ -56,6 +56,7 @@ public class DesktopBackgroundWorker implements BackgroundWorker {
@Override
public <T, V> BackgroundTaskHandler<V> handle(BackgroundTask<T, V> task) {
checkNotNull(task);
checkUIAccess();
// create task handler
DesktopTaskExecutor<T, V> taskExecutor = new DesktopTaskExecutor<>(task);
@ -68,6 +69,8 @@ public class DesktopBackgroundWorker implements BackgroundWorker {
@Override
public UIAccessor getUIAccessor() {
checkUIAccess();
return new DesktopUIAccessor();
}

View File

@ -30,12 +30,16 @@ public interface BackgroundWorker {
* @param <V> task result type
* @param task background task instance
* @return task handler
* @throws IllegalConcurrentAccessException in case of call from non UI thread
*/
@ExecutedOnUIThread
<T, V> BackgroundTaskHandler<V> handle(BackgroundTask<T, V> task);
/**
* Obtain UI access for later use from background thread. Can be invoked only from UI thread.
*
* @return ui accessor object that allows to read/write state of UI
* @throws IllegalConcurrentAccessException in case of call from non UI thread
*/
@ExecutedOnUIThread
UIAccessor getUIAccessor();

View File

@ -16,8 +16,11 @@
package com.haulmont.cuba.gui.executors;
/**
* Exception that is thrown in case of incorrect access to a shared data from a thread that does not own necessary lock.
*/
public class IllegalConcurrentAccessException extends RuntimeException {
public IllegalConcurrentAccessException() {
super("UI Shared state was accessed from background thread");
super("UI Shared state was accessed from a background thread");
}
}

View File

@ -16,8 +16,33 @@
package com.haulmont.cuba.gui.executors;
/**
* Interface that allows to read/write state of UI from background threads.
*
* @see BackgroundWorker#getUIAccessor()
*/
public interface UIAccessor {
/**
* Provides exclusive access to UI state from outside a UI event handling thread.
*
* The given runnable is executed while holding the UI lock to ensure
* exclusive access to UI state.
*
* 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 runnable is executed.
*
* @param runnable runnable
*/
void access(Runnable runnable);
/**
* Locks the UI and runs the provided Runnable right away.
*
* The given runnable is executed while holding the UI lock to ensure
* exclusive access to UI state.
*
* @param runnable runnable
*/
void accessSynchronously(Runnable runnable);
}

View File

@ -128,6 +128,8 @@ public class WebBackgroundWorker implements BackgroundWorker {
@Override
public UIAccessor getUIAccessor() {
checkUIAccess();
return new WebUIAccessor(UI.getCurrent());
}