use ReentrantLock replace sysnchronized. for ali jvm fiber wisp

This commit is contained in:
wenshao
2017-03-14 10:07:05 +08:00
parent 781f4de2b4
commit aa8459e192
6 changed files with 124 additions and 59 deletions
@@ -23,6 +23,8 @@ import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.management.JMException;
import javax.management.MBeanServer;
@@ -44,6 +46,8 @@ import com.alibaba.druid.util.JMXUtils;
@SuppressWarnings("rawtypes")
public class DruidDataSourceStatManager implements DruidDataSourceStatManagerMBean {
private final static Lock staticLock = new ReentrantLock();
public final static String SYS_PROP_INSTANCES = "druid.dataSources";
public final static String SYS_PROP_REGISTER_SYS_PROPERTY = "druid.registerToSysProperty";
@@ -62,23 +66,28 @@ public class DruidDataSourceStatManager implements DruidDataSourceStatManagerMBe
return instance;
}
public synchronized static void clear() {
Map<Object, ObjectName> dataSources = getInstances();
public static void clear() {
staticLock.lock();
try {
Map<Object, ObjectName> dataSources = getInstances();
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
for (Object item : dataSources.entrySet()) {
Map.Entry entry = (Map.Entry) item;
ObjectName objectName = (ObjectName) entry.getValue();
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
for (Object item : dataSources.entrySet()) {
Map.Entry entry = (Map.Entry) item;
ObjectName objectName = (ObjectName) entry.getValue();
if (objectName == null) {
continue;
}
if (objectName == null) {
continue;
}
try {
mbeanServer.unregisterMBean(objectName);
} catch (JMException e) {
LOG.error(e.getMessage(), e);
try {
mbeanServer.unregisterMBean(objectName);
} catch (JMException e) {
LOG.error(e.getMessage(), e);
}
}
} finally {
staticLock.unlock();
}
}
@@ -86,7 +95,8 @@ public class DruidDataSourceStatManager implements DruidDataSourceStatManagerMBe
public static Map<Object, ObjectName> getInstances() {
Map<Object, ObjectName> tmp = dataSources;
if (tmp == null) {
synchronized (DruidDataSourceStatManager.class) {
staticLock.lock();
try {
if (isRegisterToSystemProperty()) {
dataSources = getInstances0();
} else {
@@ -95,6 +105,8 @@ public class DruidDataSourceStatManager implements DruidDataSourceStatManagerMBe
dataSources = tmp = Collections.synchronizedMap(new IdentityHashMap<Object, ObjectName>());
}
}
} finally {
staticLock.unlock();
}
}