Redian新闻
>
申请石油工程和石油化工好??
avatar
申请石油工程和石油化工好??# ChemEng - 化学工程
r*l
1
近来面试,发现不少人说不清楚singleton怎么实现。在这里说一下。知道的请跳过。
1, non-lazy
avatar
o*5
2
申请石油工程和石油化工好??
哪个就业好一点,待遇好一点
avatar
r*l
3
2, lazy #1
avatar
e*m
4
都很好啊,
avatar
r*l
5
3, lazy #2
avatar
g*e
6
难道不是这种最好?

【在 r*****l 的大作中提到】
: 2, lazy #1
avatar
r*l
7
Both the 3 solutions should work. The non-lazy one is the simplest. Many
people give the solution with "synchronized" keyword but often did not get
it right.

【在 g**e 的大作中提到】
: 难道不是这种最好?
avatar
g*g
8
I think using the @Singleton annotation is the right approach.
And that should go into JSE
avatar
r*l
9
Groovy supports that, right?

【在 g*****g 的大作中提到】
: I think using the @Singleton annotation is the right approach.
: And that should go into JSE

avatar
r*y
10
hehe ...
sometimes, interviewers use this to distinguish between a newbie and a
guru.
this annotation will kill a group of interview questions

【在 g*****g 的大作中提到】
: I think using the @Singleton annotation is the right approach.
: And that should go into JSE

avatar
c*t
11
Annotation is such a powerful thing. Agree that singleton pattern
is simply not necessary, when the compiler could easily do it.

【在 g*****g 的大作中提到】
: I think using the @Singleton annotation is the right approach.
: And that should go into JSE

avatar
r*y
12
aglee...
BTW, for your #3 , there are two trivial problems:
1) instance variable = null is unnecessary. interviewer is very picky :-)
2) volatile is a bit over-killed, because object reference is usually 32 bit
on 32 bit computer, 64 on 64 tit.

【在 r*****l 的大作中提到】
: Groovy supports that, right?
avatar
r*l
13
The keyword "volatile" is the key here. Without it, the implementation is
not correct.
It's not used to ensure locking, which is enforced with "synchronized". It's
used to ensure sequential consistency.

bit

【在 r***y 的大作中提到】
: aglee...
: BTW, for your #3 , there are two trivial problems:
: 1) instance variable = null is unnecessary. interviewer is very picky :-)
: 2) volatile is a bit over-killed, because object reference is usually 32 bit
: on 32 bit computer, 64 on 64 tit.

avatar
r*y
14
you are right, otherwise, the point after "synchronized block" and before
"return instance" may generate 1+ objects.
that's a great point...

is
It's

【在 r*****l 的大作中提到】
: The keyword "volatile" is the key here. Without it, the implementation is
: not correct.
: It's not used to ensure locking, which is enforced with "synchronized". It's
: used to ensure sequential consistency.
:
: bit

avatar
r*l
15
This Wiki explains the situation:
http://en.wikipedia.org/wiki/Double-checked_locking#Usage_in_Ja
The more serious issue here is returning a partially constructed object.

【在 r***y 的大作中提到】
: you are right, otherwise, the point after "synchronized block" and before
: "return instance" may generate 1+ objects.
: that's a great point...
:
: is
: It's

avatar
g*g
16
The complexity is overrated. #3 is needed only if you are dealing
with multiple classloaders, and more than one loaders will access this
class, which is rarely the case in real world application.

【在 r*****l 的大作中提到】
: Both the 3 solutions should work. The non-lazy one is the simplest. Many
: people give the solution with "synchronized" keyword but often did not get
: it right.

avatar
r*l
17
I agree that the chance is quite small. However, bad thing may happen when
there is just one classloader.
"instance_ = new Singleton()"
may be executed in this way:
1, allocate a piece of memory and give the reference to instance_.
2, initialize Singleton object.
It's possible that after 1, and before 2, another thread comes in and get a
non null instance_ and use it.

【在 g*****g 的大作中提到】
: The complexity is overrated. #3 is needed only if you are dealing
: with multiple classloaders, and more than one loaders will access this
: class, which is rarely the case in real world application.

avatar
x*p
18
Good point!
avatar
F*n
19
What's the difference between #1 and #2 since it is a Singleton class and it
will not be initialized until being loaded by a ClassLoader?
To make the point clear, you should use a user class rather than a dedicated
Singleton Class.
avatar
S*C
20
A follow-up question, can singleton be garbage collected?
if yes, how can it guarantee the variable is consistence
when it was reloaded.
if not, why it cannot be garbage collected?
avatar
r*l
21
"you should use a user class rather than a dedicated Singleton Class"
Are you suggesting the way that Spring is working?

it
dedicated

【在 F****n 的大作中提到】
: What's the difference between #1 and #2 since it is a Singleton class and it
: will not be initialized until being loaded by a ClassLoader?
: To make the point clear, you should use a user class rather than a dedicated
: Singleton Class.

avatar
r*l
22
Once the class is loaded, the static variables are loaded and initialized.
They live while the class lives. Therefore, the references they hold live
while the class lives. Singleton instance is one of the references.
The single instance can only be GCed if there is no strong reference to it,
i.e. when the ClassLoader that loaded the singleton is GCed.

【在 S**********C 的大作中提到】
: A follow-up question, can singleton be garbage collected?
: if yes, how can it guarantee the variable is consistence
: when it was reloaded.
: if not, why it cannot be garbage collected?

avatar
S*C
23
How to implement a per-thread-singleton?
You see, the question can be endless.

,

【在 r*****l 的大作中提到】
: Once the class is loaded, the static variables are loaded and initialized.
: They live while the class lives. Therefore, the references they hold live
: while the class lives. Singleton instance is one of the references.
: The single instance can only be GCed if there is no strong reference to it,
: i.e. when the ClassLoader that loaded the singleton is GCed.

avatar
F*n
24
Factory with shared instances
Factory.getInstance(Object requestObject);
where requestObject can be stored by Factory
in a WeakReference map.

【在 S**********C 的大作中提到】
: How to implement a per-thread-singleton?
: You see, the question can be endless.
:
: ,

avatar
g*g
25
Use ThreadLocal

【在 S**********C 的大作中提到】
: How to implement a per-thread-singleton?
: You see, the question can be endless.
:
: ,

avatar
t*r
26

lazy的解法比non-lazy的有什么好处和坏处?

【在 r*****l 的大作中提到】
: 近来面试,发现不少人说不清楚singleton怎么实现。在这里说一下。知道的请跳过。
: 1, non-lazy

avatar
r*l
27
Bingo!

【在 g*****g 的大作中提到】
: Use ThreadLocal
avatar
S*C
28
Yeah, ThreadLocal is the right answer, and talking about ThreadLocal, there
is a interview questions I met before: "How do you implement the ThreadLocal
class if there is no such class in JDK"....
I was like....huh...ahh.

【在 r*****l 的大作中提到】
: Bingo!
avatar
g*e
29
there are similar questions like implement your own reentrant lock, your own
hashmap etc.
not very picky questions if the position is for senior 码工

there
ThreadLocal

【在 S**********C 的大作中提到】
: Yeah, ThreadLocal is the right answer, and talking about ThreadLocal, there
: is a interview questions I met before: "How do you implement the ThreadLocal
: class if there is no such class in JDK"....
: I was like....huh...ahh.

avatar
r*l
30
Lazy approach will hold the initialization till the last possible moment.
This reduces the application startup time, and may avoid the initiation all
together. On the other hand, it may not reveal possible failure at the
beginning.
If the constructor throws exception, you probably have to use lazy #2.
是好处,或者坏处就见仁见智了。

【在 t**r 的大作中提到】
:
: lazy的解法比non-lazy的有什么好处和坏处?

avatar
g*e
31
这几天翻effective java, josh bloch推荐了一个enum singleton的写法,他提到
"it is more concise, provides the serialization machinery for free, and
provides an
ironclad guarantee against multiple instantiation, even in the face of
sophisticated
serialization or reflection attacks. While this approach has yet to be
widely
adopted, a single-element enum type is the best way to implement a
singleton."
但是这个不是lazy的
public final class SingletonEnum {
public enum enumSingleton {
INSTANCE;

private int num = 0;

public String get() {
return "this is a enum singleton";
}

public int getNum() {
return this.num;
}

public void setNum(int w) {
this.num = w;
}
}
}

all

【在 r*****l 的大作中提到】
: Lazy approach will hold the initialization till the last possible moment.
: This reduces the application startup time, and may avoid the initiation all
: together. On the other hand, it may not reveal possible failure at the
: beginning.
: If the constructor throws exception, you probably have to use lazy #2.
: 是好处,或者坏处就见仁见智了。

avatar
r*l
32
I also saw this. While I felt it a rather weird way.

【在 g**e 的大作中提到】
: 这几天翻effective java, josh bloch推荐了一个enum singleton的写法,他提到
: "it is more concise, provides the serialization machinery for free, and
: provides an
: ironclad guarantee against multiple instantiation, even in the face of
: sophisticated
: serialization or reflection attacks. While this approach has yet to be
: widely
: adopted, a single-element enum type is the best way to implement a
: singleton."
: 但是这个不是lazy的

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。