Question

I read the ManageApplications.java of android source code and notice this 'synchronized (entry)', I wonder is it necessary here and how does it 'Bind the data efficiently'? I think a better understanding of it may solve some of my weird problem, thank you in advance.

    public View getView(int position, View convertView, ViewGroup parent) {

        ...

        // ? Bind the data efficiently with the holder
        ApplicationsState.AppEntry entry = mEntries.get(position);
        synchronized (entry) {
            holder.entry = entry;
            if (entry.label != null) {
                holder.appName.setText(entry.label);
            }
        }
        mActive.remove(convertView);
        mActive.add(convertView);

        return convertView;
    }
Was it helpful?

Solution

You are setting two values in holder from entry. If you do not synchronize, there is a chance that another thread could run getView() in between holder.entry being set and holder.appName.setText() being called. That would result in a bad state.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top