避免使用hashmap进行concurrentmodificationexception

时间:2015-06-18 20:46:29

标签: java android iterator hashmap hashset

当我运行以下代码时,我得到并发修改例外,我有2个哈希,第一个用数据填充第二个哈希,之后我必须从第二个哈希中删除数据,如果我从我获得成功的消息API,我做错了什么?

HashTask.putAll(Maps.difference(aMap, HashTask).entriesOnlyOnLeft());
                            aMap.clear();


                            class update extends TimerTask {
                                public void run() {

                                     GPSTracker gps = new GPSTracker(getActivity());
                                            String idsAlunos = "";
                                            for (Map.Entry<String, GPSEscolas> e : HashTask.entrySet()) {

                                                String id_escola = e.getKey();
                                                String ids_aluno = e.getValue().getAlunos();
                                                Float latitude = e.getValue().getLatitude();
                                                Float longitude = e.getValue().getLongitude();
                                                double latitudegps = gps.getLatitude();
                                                double longitudegps = gps.getLongitude();

                                                idsAlunos += ";" + ids_aluno;
                                                LatLng latLng = new LatLng(latitude, longitude);
                                                drawMarkerWithCircle(latLng);

                                                float[] distance = new float[2];

                                                Location.distanceBetween(mCircle.getCenter().latitude, mCircle.getCenter().longitude, latitudegps, longitudegps, distance);
                                                if (distance[0] < mCircle.getRadius()) {

                                                    try {
                                                        AlertRest mAlertRest = new AlertRest();

                                                        mAlertRest.postNotificacao(getActivity(), id_escola, idsAlunos.substring(1), latitudegps, longitudegps);

                                                        if (mAlertRest.resposta[0].equals("200")) {

                                                            mAlertRest.mJSONObject.optString("mensagem");
                                                            NotificationCompat.Builder mBuilder =
                                                                    new NotificationCompat.Builder(getActivity())
                                                                            .setSmallIcon(R.drawable.ic_clickicon)
                                                                            .setContentTitle("aaa")
                                                                            .setContentText("Seu filho ta pronto")
                                                                            .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
                                                                            .setLights(Color.RED, 3000, 3000);

                                                            Intent resultIntent = new Intent(getActivity(), MainFragment.class);

                                                            TaskStackBuilder stackBuilder = TaskStackBuilder.create(getActivity());
                                                            stackBuilder.addParentStack(getActivity());
                                                            stackBuilder.addNextIntent(resultIntent);
                                                            PendingIntent resultPendingIntent =
                                                                    stackBuilder.getPendingIntent(
                                                                            0,
                                                                            PendingIntent.FLAG_UPDATE_CURRENT
                                                                    );
                                                            mBuilder.setContentIntent(resultPendingIntent);
                                                            NotificationManager mNotificationManager =
                                                                    (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
                                                            mNotificationManager.notify(0, mBuilder.build());

                                                            HashTask.remove(id_escola);

                                                            Toast.makeText(getActivity(), mAlertRest.mJSONObject.optString("mensagem"), Toast.LENGTH_LONG).show();

                                                        } else if (mAlertRest.resposta[0].equals("422")) {
                                                            mAlertRest.mJSONObject.optString("mensagem");
                                                            Toast.makeText(getActivity(), mAlertRest.mJSONObject.optString("mensagem"), Toast.LENGTH_LONG).show();
                                                        }
                                                    } catch (Exception er) {
                                                        er.printStackTrace();
                                                    }

                                                }
                                            }
                                            }
                                        });
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            }


                            Timer timer = new Timer();
                            timer.schedule(new update(), 0, 15000);

日志:

Process: com., PID: 20574
    java.util.ConcurrentModificationException
            at java.util.HashMap$HashIterator.nextEntry(HashMap.java:806)
            at java.util.HashMap$EntryIterator.next(HashMap.java:843)
            at java.util.HashMap$EntryIterator.next(HashMap.java:841)
            at com.MainFragment$asyncTask$1$1$1update$1.run(MainFragment.java:259)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

259:for(Map.Entry e:HashTask.entrySet())

0 个答案:

没有答案