Android 线程队列使用实例——ConcurrentMap

private static ConcurrentMap> comSendTaskList=new ConcurrentHashMap<>();
initSendTimer();

 

private void initSendTask(Event event){
    comSendTaskList.put(event.getData().param.number+"",event);
}
private Thread sendThread;
private boolean sendState=true;
private void initSendTimer(){
    if(sendThread != null){return;}
    sendThread=new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                while (sendState){
                    if(comSendTaskList !=null && comSendTaskList.size() > 0){
                        for(Map.Entry> entry: comSendTaskList.entrySet()){
                            Event event =entry.getValue();
                            switch (event.getCode()){
                                case EventCode.CMD_0A0C:
                                    handEvent_0A0C(event);//借开门
                                    break;
                                case EventCode.CMD_0A06://关门断电(结束订单)
                                    handEvent_0A06(event);
                                    break;
                                default:break;
                            }

                            comSendTaskList.remove(entry.getKey());
                            SystemClock.sleep(800);
                        }                           
                    }
                    SystemClock.sleep(1500);
                }
            }catch (Exception e){

            }
        }
    });

    sendThread.start();
}
private void closeSendTh(){
    if(sendThread !=null){
        sendThread.stop();
        sendThread=null;
    }
}
initSendTask(event);//先缓存到队列集合

 

你可能感兴趣的:(Android 线程队列使用实例——ConcurrentMap)