b****u 发帖数: 1130 | 1 看到一个奇特的类, 想请教.
public class SpecialClass{
private A a;
private boolean b;
private Object o;
public SpecialClass (A a) {
this.a = a;
}
synchronized Object z() {
if (!b) {
o = a.y();
b = true;
}
return o;
}
public interface A {
Object y();
}
}
1. 类的功能 干什么用
2. 如何generify这个类.
3如何提高在并发环境下性能 |
g**e 发帖数: 6127 | 2 这个是考lazy initialized singleton?
【在 b****u 的大作中提到】 : 看到一个奇特的类, 想请教. : public class SpecialClass{ : private A a; : private boolean b; : private Object o; : public SpecialClass (A a) { : this.a = a; : } : synchronized Object z() { : if (!b) {
|
q*********u 发帖数: 280 | 3
看到一个奇特的类, 想请教.
public class SpecialClass{
private A a;
private boolean b;
private T o;
public SpecialClass (A a) {
this.a = a;
}
synchronized T z() {
if (!b) {
o = a.y();
b = true;
}
return o;
}
public interface A {
T y();
}
}
1. 类的功能 干什么用
Factory
2. 如何generify这个类.
3如何提高在并发环境下性能
按singleton的标准写z()这个method.
【在 b****u 的大作中提到】 : 看到一个奇特的类, 想请教. : public class SpecialClass{ : private A a; : private boolean b; : private Object o; : public SpecialClass (A a) { : this.a = a; : } : synchronized Object z() { : if (!b) {
|
b****u 发帖数: 1130 | 4 十分感谢楼上两位,但一些细节还是不明白,不太确定如何用这个类.
需要有其他类, 如 class B implements SpecialClass.A, 用Class B 这个类实现接
口,对
吧?
另外我们可不可以理解SpecialClass 为 "AbstractSingleton"?
【在 q*********u 的大作中提到】 : : 看到一个奇特的类, 想请教. : public class SpecialClass{ : private A a; : private boolean b; : private T o; : public SpecialClass (A a) { : this.a = a; : } : synchronized T z() {
|
m******t 发帖数: 2416 | 5 This looks more like a simple and naive cache implementation to me.
【在 b****u 的大作中提到】 : 看到一个奇特的类, 想请教. : public class SpecialClass{ : private A a; : private boolean b; : private Object o; : public SpecialClass (A a) { : this.a = a; : } : synchronized Object z() { : if (!b) {
|
s******e 发帖数: 493 | 6 Hard to say what this class is for.
But the main purpose of the whole things seems to guarantee that
o = a.y();
will be called only once for the delegated class object A.
It is kind of lazy initialization of Object O. Of course o can be used as a
cache object or whatever. |
g*****g 发帖数: 34805 | 7 +1, a cache assuming object initialization is heavy.
For the most efficient implementation, check memorizer
in "Java Concurrency in Practice"
【在 m******t 的大作中提到】 : This looks more like a simple and naive cache implementation to me.
|
q*********u 发帖数: 280 | 8 yeah, an excellent book
+1, a cache assuming object initialization is heavy.
For the most efficient implementation, check memorizer
in "Java Concurrency in Practice"
【在 g*****g 的大作中提到】 : +1, a cache assuming object initialization is heavy. : For the most efficient implementation, check memorizer : in "Java Concurrency in Practice"
|