Skip to content

Commit

Permalink
添加并发控制
Browse files Browse the repository at this point in the history
  • Loading branch information
黄志磊 committed Jan 13, 2016
1 parent bfb8c56 commit b91eb9b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
Expand Up @@ -53,8 +53,13 @@ public static <T> T getInstance(Class<T> clazz) {
public static <T> List<T> getInstances(Class<T> clazz){ public static <T> List<T> getInstances(Class<T> clazz){
ConcurrentMap<String, Object> objMap = objectsCachedMap.get(clazz); ConcurrentMap<String, Object> objMap = objectsCachedMap.get(clazz);
if (objMap == null) { if (objMap == null) {
objMap = Maps.newConcurrentMap(); synchronized (clazz) {
objectsCachedMap.put(clazz, objMap); objMap = objectsCachedMap.get(clazz);
if(objMap == null){
objMap = Maps.newConcurrentMap();
objectsCachedMap.put(clazz, objMap);
}
}
} }
objMap = objectsCachedMap.get(clazz); objMap = objectsCachedMap.get(clazz);
if(!objMap.isEmpty()){ if(!objMap.isEmpty()){
Expand All @@ -69,11 +74,9 @@ public static <T> List<T> getInstances(Class<T> clazz){
if (!clazzMap.isEmpty()) { //防止一个实例对象被初始化多次 if (!clazzMap.isEmpty()) { //防止一个实例对象被初始化多次
synchronized (clazz) { synchronized (clazz) {
try { try {

if(!objMap.isEmpty()){ if(!objMap.isEmpty()){
return Lists.newArrayList((List<T>)objMap.values()); return Lists.newArrayList((List<T>)objMap.values());
} }

Iterator<Entry<String, Class<?>>> iter = clazzMap.entrySet().iterator(); Iterator<Entry<String, Class<?>>> iter = clazzMap.entrySet().iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
Entry<String, Class<?>> entry = iter.next(); Entry<String, Class<?>> entry = iter.next();
Expand Down Expand Up @@ -205,7 +208,13 @@ private static void loadFile(Class<?> type) {
} catch (Throwable t) { } catch (Throwable t) {
log.error("", "Exception when load extension class(interface: " + type + ", description file: " + fileName + ").", t); log.error("", "Exception when load extension class(interface: " + type + ", description file: " + fileName + ").", t);
} }
clazzCacheMap.put(type, map); synchronized (type) {
Map<String, Class<?>> oldMap = clazzCacheMap.get(type);
if(oldMap==null){
clazzCacheMap.put(type, map);
}
}

} }


public static String toLowerCaseFirstOne(String s) { public static String toLowerCaseFirstOne(String s) {
Expand Down
@@ -1,13 +1,42 @@
package com.shinemo.mpush.tools.spi; package com.shinemo.mpush.tools.spi;




import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.junit.Ignore;
import org.junit.Test;

import com.shinemo.mpush.tools.spi.test.TestService; import com.shinemo.mpush.tools.spi.test.TestService;



public class SpiTest { public class SpiTest {


public static void main(String[] args) { private static Executor pool = Executors.newCachedThreadPool();

@Ignore
@Test
public void baseTest(){
TestService testService = ServiceContainer.getInstance(TestService.class); TestService testService = ServiceContainer.getInstance(TestService.class);
System.out.println(testService.sayHi(" huang")); System.out.println(testService.sayHi(" huang"));
} }

@Test
public void mulThreadTest() throws InterruptedException{
pool.execute(new Worker());
pool.execute(new Worker());
Thread.sleep(Integer.MAX_VALUE);
}



private static final class Worker implements Runnable{

@Override
public void run() {
TestService testService = ServiceContainer.getInstance(TestService.class);
System.out.println(testService.sayHi(" huang"));
}

}

} }

0 comments on commit b91eb9b

Please sign in to comment.