当多个线程访问同一个对象时,如何从DOM对象安全地获取数据

时间:2018-02-13 13:29:04

标签: java xml multithreading dom xml-parsing

我有多个线程试图读取相同的DOM对象并同时运行。 但我听说,DOM不是线程安全的,在某处我读到如果我们克隆文档,克隆的文档将是线程安全的。 这就是我想要的尝试

  public class AppList extends AppCompatActivity {
    ListView listView;
    ListAdapter listAdapter;
    String TAG = "App List";
    List<ApplicationInfo> packages;
    SharedPreferences.Editor editor;
    SharedPreferences sharedPreferences;


    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.applistonly);
        listView = (ListView) findViewById(R.id.listforapps);

        editor = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit();
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

        final PackageManager pm = getPackageManager();
//get a list of installed apps.

        packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
        Log.i(TAG, "List Pack " + packages.size());

        listAdapter = new ListAdapter(AppList.this, packages);
        listView.setAdapter(listAdapter);
        for (ApplicationInfo packageInfo : packages) {
            Log.d(TAG, "Installed package :" + packageInfo.packageName);

        }


    }





    public class ListAdapter extends BaseAdapter {
        Activity activity;
        List<ApplicationInfo> applist;
        LayoutInflater layoutInflater;
        ViewHolder viewHolder;

        ListAdapter(Activity activity1, List<ApplicationInfo> list) {
            this.activity = activity1;
            this.applist = list;
        }


        @Override
        public int getCount() {

            Log.i(TAG, "Adapter List " + applist.size());

            return applist.size();
        }

        @Override
        public Object getItem(int i) {
            return applist.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        private class ViewHolder {
            private TextView appname;
            private ImageView appimage;
            private CheckBox appsel;

        }


        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if (layoutInflater == null) {
                layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }

            if (view == null) {
                //Custom list layout must be set
                assert layoutInflater != null;
                view = layoutInflater.inflate(R.layout.applistcustom, null);
                viewHolder = new ViewHolder();

                viewHolder.appname = (TextView) view.findViewById(R.id.appname);
                viewHolder.appimage = (ImageView) view.findViewById(R.id.appimage);
                viewHolder.appsel = (CheckBox) view.findViewById(R.id.selapp);

            }
                //viewHolder.appname.setText(applist.get(i).packageName);


                viewHolder.appname.setText(applist.get(i).loadLabel(getPackageManager()));
                //    viewHolder.appname.setText(GetAppName(applist.get(i)));
                viewHolder.appimage.setImageDrawable(getPackageManager().getApplicationIcon(applist.get(i)));



            return view;

        }


    }

}

现在我将允许我的线程从copiedDocument而不是originalDocument中读取。

那么,现在我的代码是否在线程安全运行?有谁可以请确认。

0 个答案:

没有答案