You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ji...@apache.org on 2019/04/16 07:46:41 UTC

[incubator-iotdb] branch instrumentation_for_hashmap updated: add apache header and strengthen synchronization

This is an automated email from the ASF dual-hosted git repository.

jiangtian pushed a commit to branch instrumentation_for_hashmap
in repository https://gitbox.apache.org/repos/asf/incubator-iotdb.git


The following commit(s) were added to refs/heads/instrumentation_for_hashmap by this push:
     new cbba669  add apache header and strengthen synchronization
cbba669 is described below

commit cbba669202821af49e988016b546e4edfda019d1
Author: 江天 <jt...@163.com>
AuthorDate: Tue Apr 16 15:45:25 2019 +0800

    add apache header and strengthen synchronization
---
 .../iotdb/tsfile/expr/HashMapInstrumentor.java     | 47 ++++++++++++++++------
 1 file changed, 35 insertions(+), 12 deletions(-)

diff --git a/tsfile/src/main/java/org/apache/iotdb/tsfile/expr/HashMapInstrumentor.java b/tsfile/src/main/java/org/apache/iotdb/tsfile/expr/HashMapInstrumentor.java
index abc0fd7..8cdd818 100644
--- a/tsfile/src/main/java/org/apache/iotdb/tsfile/expr/HashMapInstrumentor.java
+++ b/tsfile/src/main/java/org/apache/iotdb/tsfile/expr/HashMapInstrumentor.java
@@ -1,3 +1,22 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
 package org.apache.iotdb.tsfile.expr;
 
 
@@ -16,23 +35,27 @@ public class HashMapInstrumentor {
   private static Thread reportTimer;
 
   public static void incCount(Class cls) {
-    AtomicLong count = newCounter.computeIfAbsent(cls, k -> new AtomicLong(0));
-    count.addAndGet(1);
+    synchronized (newCounter) {
+      AtomicLong count = newCounter.computeIfAbsent(cls, k -> new AtomicLong(0));
+      count.addAndGet(1);
+    }
   }
 
   public static void report() {
-    StringBuilder stringBuilder = new StringBuilder("\n");
-    long tot = 0;
-    for (Entry<Class, AtomicLong> entry : newCounter.entrySet()) {
-      long count = entry.getValue().get();
-      if (count < 100) {
-        continue;
+    synchronized (newCounter) {
+      StringBuilder stringBuilder = new StringBuilder("\n");
+      long tot = 0;
+      for (Entry<Class, AtomicLong> entry : newCounter.entrySet()) {
+        long count = entry.getValue().get();
+        if (count < 100) {
+          continue;
+        }
+        stringBuilder.append(entry.getKey().getSimpleName()).append(":").append(count).append("\n");
+        tot += count;
       }
-      stringBuilder.append(entry.getKey().getSimpleName()).append(":").append(count).append("\n");
-      tot += count;
+      stringBuilder.append("total:").append(tot);
+      LOGGER.info("HashMap usage:\n{}", stringBuilder);
     }
-    stringBuilder.append("total:").append(tot);
-    LOGGER.info("HashMap usage:\n{}", stringBuilder);
   }
 
   public static void start() {