日期:2014-05-20 浏览次数:20736 次
public class MapperInstances {
private static MapperInstances instance;
private MapperInstances() {}
public static MapperInstances getInstance() {
if(instance == null) {
instance = new MapperInstances();
}
return instance;
}
}
weka.core.Instances in = MapperInstances.getInstance().mapToWeka(data);
第一种:
[java] view plaincopyprint?
01.public class Singleton2 {
02.
03. private Singleton2(){
04. System.out.println("This is Singleton2's instance.");
05. };
06.
07. private static Singleton2 instance = null;
08.
09. public static Singleton2 getInstance(){
10. if(instance == null) {
11. instance = new Singleton2();
12. }
13. return instance;
14. }
15.}
这种情况未加锁,可能会产生数据错误,比如两个同时新生成的对象,一个把对象数据改变了,而另一个使用的没改变之前的。
第二种:
[java] view plaincopyprint?
01.public class Singleton1 {
02.
03. private Singleton1(){
04. System.out.println("This is Singleton1's instance.");
05. }
06.
07. private static Singleton1 instance = null;
08.
09. public static Singleton1 getInstance2() {
10. if(instance == null){ //1
11. synchronized (Singleton1.class) { //2
12. if(instance == null) {
13. instance = new Singleton1();
14. }
15. }
16. }