Skip to content

Instantly share code, notes, and snippets.

@fge
Created February 11, 2015 14:52
Show Gist options
  • Select an option

  • Save fge/e26cf30275a067de96ae to your computer and use it in GitHub Desktop.

Select an option

Save fge/e26cf30275a067de96ae to your computer and use it in GitHub Desktop.
/**
* Run a task in the background producing a value; schedule a task consuming
* that value to run on the UI thread
*
* @param supplier the background task producing a value
* @param consumer the UI thread task consuming that value
* @param <T> type parameter of the produced/consume value
*/
public <T> void compute(
@OnBackgroundThread final Supplier<? extends T> supplier,
@OnUiThread final Consumer<? super T> consumer)
{
Objects.requireNonNull(supplier);
Objects.requireNonNull(consumer);
executor.submit(() -> {
final T t = supplier.get();
frontExecutor.execute(() -> consumer.accept(t));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment