AsyncTasks and thread safe data objects?

Lets say that my activity has a list object containing material. This activity starts with several AsyncTasks. When these tasks are completed, they change this list (add material, for example) to their postExecute () methods. Should this list be thread safe?

My bet is that this is not necessary, because the code in the postExecute () methods runs in the user interface thread sequentially. Therefore, they cannot change the list in parallel. Is it correct?

+3
source share
1 answer

Yes you are right. postExecute()runs in the user interface thread, so all of your AsyncTasks will update your list sequentially (and not at the same time).

In other situations, I would advise you to take a look at synchronizedListJava Collections.

+2
source

Source: https://habr.com/ru/post/1790713/


All Articles