Zsombor Erdődy-Nagy
Zsombor Erdődy-Nagy

Reputation: 16914

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 the postExecute() methods run in the UI thread, sequentially. So they don't get to modify the list in parallel. Is this correct?

Upvotes: 3

Views: 1263

Answers (1)

goncalossilva
goncalossilva

Reputation: 1860

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.

Upvotes: 2

Related Questions