I have a worker thread that creates a runnable object and calls runOnUiThread on it, because it deals with Views and controls. I\'d like to use the result of the work of the
A solution might be to leverage Java's FutureTask which has the benefit that someone else has already dealt with all the potential concurrency issues for you:
public void sample(Activity activity) throws ExecutionException, InterruptedException {
Callable callable = new Callable() {
@Override
public Void call() throws Exception {
// Your task here
return null;
}
};
FutureTask task = new FutureTask<>(callable);
activity.runOnUiThread(task);
task.get(); // Blocks
}
You can even return a result from the main thread by replacing Void with something else.