定义一个类,类中有一个方法如下:
public static PropertyAccess getInstance() throws IOException {
if ( instance == null ) {
synchronized (PropertyAccess.class) {
if ( instance == null ) {
try {
instance = new PropertyAccess();
} catch (IOException e) {
throw e;
}
}
}
}
return instance;
}synchronized (PropertyAccess.class)是什么意思?哪位大神知道的给小弟解释下,不胜感激!
public static PropertyAccess getInstance() throws IOException {
if ( instance == null ) {
synchronized (PropertyAccess.class) {
if ( instance == null ) {
try {
instance = new PropertyAccess();
} catch (IOException e) {
throw e;
}
}
}
}
return instance;
}synchronized (PropertyAccess.class)是什么意思?哪位大神知道的给小弟解释下,不胜感激!
用处:锁住PropertyAccess.class,保证只有当前线程能执行后面大括号里面的代码,其他线程执行到这里,将会挂起等待。
用处:锁住PropertyAccess.class,保证只有当前线程能执行后面大括号里面的代码,其他线程执行到这里,将会挂起等待。
synchronized (PropertyAccess.class) 就是在 PropertyAccess.class 上加个锁。class文件在内存里是唯一,这样就可以同步了