AsyncTasks and thread-safe data objects?
Lets say my Activity has a list object containing stuff. This Activity starts off several AsyncTasks. When these tasks finish, they modify this list (add stuff in it for example), in their postExecute() methods. Should this list be thread-safe?
My bet is that it's not neccesary, because the code in 开发者_如何学Cthe postExecute() methods run in the UI thread, sequentially. So they don't get to modify the list in parallel. Is this correct?
Yes, you are correct. The postExecute()
is executed in the UI thread so all your AsyncTasks will update your list sequentially (and not at the same time).
In other situations I'd advise you to have a look into synchronizedList
of Java's Collections
.
精彩评论