避免多线程统计数据错误

  1 import java.io.File;
  2 import java.io.FileNotFoundException;
  3 import java.io.FileWriter;
  4 import java.io.IOException;
  5 import java.util.Iterator;
  6 import java.util.Map.Entry;
  7 import java.util.Set;
  8 import java.util.concurrent.ConcurrentHashMap;
  9 import java.util.concurrent.atomic.AtomicInteger;
 10 
 11 import org.apache.commons.io.IOUtils;
 12 import org.apache.log4j.Logger; 16 
 17 public class CountSendSignalingRows {
 18 
 19     final private static Logger logger = Logger.getLogger(CountSendSignalingRows.class);
 20 
 21     static String appHome = projectConfiguration.getInstance().getString(projectConfiguration.KEY_APP_HOME_DIR);
 22     static String recvFilePathName = appHome + File.separator + "cfg" + File.separator + "receiveSignallingNum.txt";
 23     static String sendFilePathName = appHome + File.separator + "cfg" + File.separator + "forwardSignallingNum.txt";
 24     ConcurrentHashMap<String, AtomicInteger> countRecvSignalingNumMap = new ConcurrentHashMap<String, AtomicInteger>();
 25     ConcurrentHashMap<String, AtomicInteger> countSendSignalingNumMap = new ConcurrentHashMap<String, AtomicInteger>();
 26     String recvFileName = "";
 27     String sendFileName = "";
 28 
 29     private static class LazyHolder {
 30         private static final CountSendSignalingRows INSTANCE = new CountSendSignalingRows();
 31     }
 32 
 33     public static final CountSendSignalingRows getInstance() {
 34         return LazyHolder.INSTANCE;
 35     }
 36 
 37     private Thread t = null;
 38 
 39     private CountSendSignalingRows() {
 40         t = new Thread(new Runnable() {
 41 
 42             @Override
 43             public void run() {
 44                 while (true) {
 45                     try {
 46                         // write recv map
 47                         writeFile(recvFilePathName, countRecvSignalingNumMap);
 48                         // write send map
 49                         writeFile(sendFilePathName, countSendSignalingNumMap);
 50                         Thread.sleep(30000);
 51                     } catch (Exception e) {
 52                         
 53                     }
 54                 }
 55             }
 56         });
 57         
 58         t.start();
 59         
 60     }
 61 
 62     public void countRecvSignalingRows(String eventName) {
 63 
 64         countRecvSignalingRows(eventName, countRecvSignalingNumMap);
 65 
 66     }
 67 
 68     public void countSendSignalingRows(String eventName) {
 69 
 70         countRecvSignalingRows(eventName, countSendSignalingNumMap);
 71 
 72     }
 73 
 74     private void countRecvSignalingRows(String eventName,
 75             ConcurrentHashMap<String, AtomicInteger> countRecvSignalingNumMap) {
 76         AtomicInteger zeroAi = new AtomicInteger(0);
 77         AtomicInteger ai = countRecvSignalingNumMap.putIfAbsent(eventName, zeroAi);
 78         if (ai == null) {
 79             ai = zeroAi;
 80         }
 81         ai.incrementAndGet();
 82     }
 83 
 84     private void writeFile(String pathname, ConcurrentHashMap<String, AtomicInteger> countSignalingNumMap) {
 85         FileWriter fw = null;
 86         FileWriter fileWriter = null;
 87         File file = new File(pathname);
 88         try {
 89             if (!file.exists()) {
 90                 file.createNewFile();
 91             }
 92             if (countSignalingNumMap == null || countSignalingNumMap.isEmpty()) {
 93                 fileWriter = new FileWriter(file);
 94                 fileWriter.write("");
 95                 fileWriter.flush();
 96             }
 97         } catch (IOException e) {
 98             e.printStackTrace();
 99         } finally {
100             IOUtils.closeQuietly(fileWriter);
101         }
102 
103         try {
104 
105             String line = System.getProperty("line.separator");
106             StringBuffer str = new StringBuffer();
107             fw = new FileWriter(pathname);
108             Iterator<Entry<String, AtomicInteger>> iterator = countSignalingNumMap.entrySet().iterator();
109             while (iterator.hasNext()) {
110                 Entry<String, AtomicInteger> entry = iterator.next();
111                 str.append(entry.getKey() + " : " + entry.getValue()).append(line);
112             }
113             fw.write(str.toString());
114             fw.flush();
115             logger.debug("The statistics are written successfully");
116         } catch (FileNotFoundException e) {
117             e.printStackTrace();
118         } catch (IOException e) {
119             e.printStackTrace();
120         } finally {
121 
122             IOUtils.closeQuietly(fw);
123         }
124 
125     }
126 }

 

posted @ 2017-12-10 20:59  追逐新梦想  阅读(488)  评论(0编辑  收藏  举报