问题描述
整个生产数据的和处理数据的过程都需要快速。希望大侠们帮忙看看,帮小弟指出有什么需要优化的地方和容易出问题的地方//线程1---生产数据publicclassDataForWlanextendsThread{privateCardReaderForWlanrr;publicstaticSet<String>carIdAndTimeSet=Collections.synchronizedSet(newHashSet<String>());publicSet<String>getCarIdAndTimeSet(){returncarIdAndTimeSet;}publicvoidsetCarIdAndTimeSet(Set<String>carIdAndTimeSet){this.carIdAndTimeSet=carIdAndTimeSet;}//初始化线程类privatevoidinit(){try{rr=newCardReaderForWlan();//创建无线读卡连接}catch(ReaderExceptione){e.printStackTrace();return;}if(rr==null){return;}}publicsynchronizedvoidrun(){init();try{while(true){TagReadData[]trd=rr.MultiReadCard();//不断的读卡但不一定每时每刻都有数据产生if(trd.length>0){readCar(trd);}//this.sleep(5);}}catch(Exceptione){this.stop();e.printStackTrace();}}/***处理需要的数据*/publicvoidreadCar(TagReadData[]trd){try{inttrdLength=trd.length;if(trdLength<=0){return;}for(inti=0;i<trdLength;i++){StringcardID=trd[i].EPCHexstr();//卡IDStringcarIdAndTime=cardID+","+System.currentTimeMillis()/1000;//用于线程2处理数据用carIdAndTimeSet.add(carIdAndTime);//短时间内会读一张卡几次,但只需要一结果即可}}catch(Exceptione){e.printStackTrace();}}/***清空数据*/publicvoidclearcarIdAndTimeSet(){carIdAndTimeSet.clear();}}
线程2publicclassMainJFrameextendsjavax.swing.JFrameimplementsActionListener,Runnable{publicSet<String>carIAndTime=null;//读到的卡号存放publicSet<String>cardIdlist=newHashSet<String>();//显示面板存放卡号privateMap<String,Long>map=newConcurrentHashMap<String,Long>();publicDataForWlandfw=newDataForWlan();//无线读卡器线程publicvoidrun(){while(true){try{carIAndTime=newHashSet<String>();carIAndTime.addAll(dfw.getCarIdAndTimeSet());//将线程1中的值赋值给carIAndTimedfw.clearcarIdAndTimeSet();//清空数据if(carIAndTime.size()!=0){Iterator<String>iterator=carIAndTime.iterator();while(iterator.hasNext()){StringcardTime=iterator.next();String[]strs=cardTime.split("[,]");StringcardID=strs[0];//卡IDuser=listAllUsers.get(strs[0]);//根据卡ID得到签到人员if(user!=null){longtime=Long.valueOf(strs[1]);Iterator<String>keys=map.keySet().iterator();if(keys.hasNext()){//以下是做业务处理可以忽略booleanre=isExist(map,cardID);if(re){//存在longaftertime=map.get(cardID);//前一个时间if(time-aftertime>30){map.put(strs[0],Long.valueOf(strs[1]));cardIdlist.add(strs[0]);}}else{map.put(strs[0],Long.valueOf(strs[1]));cardIdlist.add(strs[0]);}}else{map.put(strs[0],Long.valueOf(strs[1]));cardIdlist.add(strs[0]);}setCarIdOfTime(cardIdlist);//将得到数据,进行业务处理cardIdlist.clear();iterator.remove();//后来加的}}}Thread.currentThread().sleep(600);}catch(InterruptedExceptione){e.printStackTrace();}}}publicbooleanisExist(Map<String,Long>map,Stringabc){booleanisexit=false;Iterator<String>keys=map.keySet().iterator();if(keys.hasNext()){while(keys.hasNext()){Stringkey=(String)keys.next();if(abc.equals(key)){//判断此卡号在map中不存在returntrue;}}}returnisexit;}}
运行几个小时后报错如下。有的时候几十分钟也会报这个错误这个错误也是我最常看到的,经常出现。Exceptioninthread"Thread-4"java.util.ConcurrentModificationExceptionatjava.util.HashMap$HashIterator.nextEntry(UnknownSource)atjava.util.HashMap$KeyIterator.next(UnknownSource)atjava.util.AbstractCollection.addAll(UnknownSource)atindexframes.MainJFrame.run(MainJFrame.java:380)atjava.lang.Thread.run(UnknownSource)//380行就是线程2中的carIAndTime.addAll(dfw.getCarIdAndTimeSet());
但是在线程2中添加下面代码后已经跑了12个小时。还在测试中iterator.remove();//后来加的
感觉这边remove还是有风险