Synchronization with the main thread in Java
public class MainActivity extends AppCompatActivity {
//...
public void onClick(View v) {
new CalcTask().execute();
}
int Add(int a, int b) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return a + b;
}
private class CalcTask extends AsyncTask<Void, Void, Integer> {
@Override
protected Integer doInBackground(Void... params) {
return Add(3, 5);
}
@Override
protected void onPostExecute(Integer result) {
//synchronization with the main thread
//for update user interface
mResultTextView.setText(result.toString());
}
}
}
- Log in to post comments