Currently, the borealis code runs on a single thread. Users can do asynchronous work on a separate thread if they wish, but they have to synchronize the operation manually with the main thread (or "UI thread").
The point of an "asynchronous background task" is to provide a way to automatically synchronize the background work with the main thread, similarly to the (late) AsyncTask class of Android. The design is actually copy pasted from AsyncTask 🙈
The task can be implemented as a template class with three arguments:
The type of the parameters given to the task when you execute it (the input)
The type of the data returned to the UI thread for progress report (for automatic synchronization)
The type of the data returned by the task (the output)
The class contains multiple methods, all of them synchronized:
onPreExecute: executed on the UI thread before the task is started
run: executed on a separate thread, can contain a parameter of type corresponding to the first template parameter
onProgressUpdate (synchronized with reportProgress): executed on the UI thread to update the UI according to the reported progress (type of the second template parameter)
reportProgress (synchronized with onProgressUpdate): allows to safely report progress from the background thread to the main thread
onPostExecute executed on the UI thread when run() returns, can contain a parameter of type corresponding to the third template parameter
When you start the task:
onPreExecute is called
run is posted on a new thread
run repeatedly calls reportProgress
Meanwhile, the UI thread checks the state of the task every frame (with synchronization):
call onProgressUpdate if there is a progress report pending (this will block reportProgress)
call onPostExecute and free the task if run has finished
Currently, the borealis code runs on a single thread. Users can do asynchronous work on a separate thread if they wish, but they have to synchronize the operation manually with the main thread (or "UI thread").
The point of an "asynchronous background task" is to provide a way to automatically synchronize the background work with the main thread, similarly to the (late)
AsyncTask
class of Android. The design is actually copy pasted fromAsyncTask
🙈The task can be implemented as a template class with three arguments:
The class contains multiple methods, all of them synchronized:
onPreExecute
: executed on the UI thread before the task is startedrun
: executed on a separate thread, can contain a parameter of type corresponding to the first template parameteronProgressUpdate
(synchronized withreportProgress
): executed on the UI thread to update the UI according to the reported progress (type of the second template parameter)reportProgress
(synchronized withonProgressUpdate
): allows to safely report progress from the background thread to the main threadonPostExecute
executed on the UI thread whenrun()
returns, can contain a parameter of type corresponding to the third template parameterWhen you start the task:
onPreExecute
is calledrun
is posted on a new threadrun
repeatedly callsreportProgress
Meanwhile, the UI thread checks the state of the task every frame (with synchronization):
onProgressUpdate
if there is a progress report pending (this will blockreportProgress
)onPostExecute
and free the task ifrun
has finished