/**
* Factory to generate weak referenced singleton instances. <p/> Sample Usage:
*
* <pre>
* class ClassA {
* //contents here
* }
*
* class UserClass {
* public void classAUser() {
* ClassA classA = (CarA) SingletonFactory.getInstance()
* .getSingletonInstanceOf(ClassA.class);
* }
* }
* </pre>
*
* @see java.lang.ref
* @see java.util.WeakHashmap
*
*/ public class SingletonFactory { private static Log log = LogFactory.getLog(SingletonFactory.class); private static Map map = new WeakHashMap(); private static SingletonFactory instance = new SingletonFactory();
private SingletonFactory() {
//intended
}
/**
* @return instance of SingletonFacotry
*/ public static SingletonFactory getInstance() { return instance;
}
/**
* Synchronzied method to instantiate object or get if exist.
*
* @param clazz Class for which to get instance. The method uses reflection
* to instantiate the class, so clazz should have a default
* constructor accessable by this factory.
* @return Instance generated or got from registry
* @see java.lang.Reflection
*/ public synchronized// TODO could use volatile and/or Semaphore to improve
// performance in JDK5.0+
Object getSingletonInstanceOf(Class clazz) {
if (null == clazz) throw new IllegalArgumentException("need a valid Class");
Object object = map.get(clazz.getName()); if (null == object) { try {
object = clazz.newInstance();
} catch (InstantiationException e) {
log.fatal("cannot instantiate Class " + clazz.getName()); throw new RuntimeException(e);
} catch (IllegalAccessException e) {
log.fatal("illegal access to Class " + clazz.getName()); throw new RuntimeException(e);
}
map.put(clazz.getName(), object);
log.debug("new object of " + clazz.getName() + "created");
} return object;
}
}
(#2858048@0) Last Updated: 2006-3-22 This post has been archived. It cannot be replied.
If CarA is orphan but still in the JVM due to undetermined garbage collector behavior, do you still want Thread2 detects it?
-yuanzidan(原子弹);
2006-3-21(#2856226@0)
No. What I need is, for Thread 2, find out whether CarA is being invoked by another thread.
-sailor(送你一朵西兰花);
2006-3-21(#2856249@0)
is there any way to access JVM's heap or something like that?
-objectspace(New File Sys);
2006-3-21(#2856261@0)
One of CarA's method is being called by another thread not nesessay thread1? how about you make CarA a monitor, in thread2 check holdslock?
-yuanzidan(原子弹);
2006-3-21(#2856332@0)
does Java support such low level object recognization? I dont know Java well, maybe some kind reflection may help. Does Java support this?
-objectspace(New File Sys);
2006-3-21(#2856254@0)
可以搞一个pool池, 或者类似的做法, thread create carA 后,把carA放入一个collection中
-guoluworker(锅炉工);
2006-3-21(#2856267@0)
why not singleton? when count goes down to 0, force GC.
-objectspace(New File Sys);
2006-3-21(#2856333@0)
if you read the code for JDK implementation, System.gc() does not do the garbage collection any more
-blackscraper(橡皮鱼);
2006-3-21{161}(#2856513@0)
JDK with version higher than 1.1 NEVER EVER guarantees the call into System.gc() tiggers the garbage collection. In fact, you can NOT force a garbage collection.
Use pool with weak reference.
-ra_95(凡信的,都将归于荣耀);
2006-3-21(#2856417@0)
support this one
-informix(informix);
2006-3-21(#2856472@0)
Thanks~~~ I believe this is the correct direction.
-sailor(送你一朵西兰花);
2006-3-21(#2856479@0)
if u dont care about life time, and dont want to reuse it, how about use a global counter, add code in ctor, that's it. not OO approach though.
-objectspace(New File Sys);
2006-3-21(#2856496@0)
if you want to know excat how many, use finalize ()
-objectspace(New File Sys);
2006-3-21(#2856506@0)
No wonder you cannot beat a "造假人士" ... lol ... 如果你到我这面食,你肯定不过...随便找个retraining的都比你强太多。找不到工作的,要么象你,要么就是太老实。对了,我就是一个靠假简历改行的,大家认识一下。LOL.....
-ra_95(凡信的,都将归于荣耀);
2006-3-21(#2856515@0)
haha, before u LOL... according to his original requirement, he only wants to know at a certain time, how many Cars are created.
-objectspace(New File Sys);
2006-3-21{128}(#2856531@0)
He event does not care about whether afterwards, the existing car will disappear. what's use for your pool though in this case?
/**
* Factory to generate weak referenced singleton instances. <p/> Sample Usage:
*
* <pre>
* class ClassA {
* //contents here
* }
*
* class UserClass {
* public void classAUser() {
* ClassA classA = (CarA) SingletonFactory.getInstance()
* .getSingletonInstanceOf(ClassA.class);
* }
* }
* </pre>
*
* @see java.lang.ref
* @see java.util.WeakHashmap
*
*/ public class SingletonFactory { private static Log log = LogFactory.getLog(SingletonFactory.class); private static Map map = new WeakHashMap(); private static SingletonFactory instance = new SingletonFactory();
private SingletonFactory() {
//intended
}
/**
* @return instance of SingletonFacotry
*/ public static SingletonFactory getInstance() { return instance;
}
/**
* Synchronzied method to instantiate object or get if exist.
*
* @param clazz Class for which to get instance. The method uses reflection
* to instantiate the class, so clazz should have a default
* constructor accessable by this factory.
* @return Instance generated or got from registry
* @see java.lang.Reflection
*/ public synchronized// TODO could use volatile and/or Semaphore to improve
// performance in JDK5.0+
Object getSingletonInstanceOf(Class clazz) {
if (null == clazz) throw new IllegalArgumentException("need a valid Class");
Object object = map.get(clazz.getName()); if (null == object) { try {
object = clazz.newInstance();
} catch (InstantiationException e) {
log.fatal("cannot instantiate Class " + clazz.getName()); throw new RuntimeException(e);
} catch (IllegalAccessException e) {
log.fatal("illegal access to Class " + clazz.getName()); throw new RuntimeException(e);
}
map.put(clazz.getName(), object);
log.debug("new object of " + clazz.getName() + "created");
} return object;
}
}