o*5
2 楼
申请石油工程和石油化工好??
哪个就业好一点,待遇好一点
哪个就业好一点,待遇好一点
r*l
3 楼
2, lazy #1
e*m
4 楼
都很好啊,
r*l
5 楼
3, lazy #2
g*g
8 楼
I think using the @Singleton annotation is the right approach.
And that should go into JSE
And that should go into JSE
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.
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.
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
"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
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
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
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.
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.
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.
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.
x*p
18 楼
Good point!
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.
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.
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?
if yes, how can it guarantee the variable is consistence
when it was reloaded.
if not, why it cannot be garbage collected?
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.
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.
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?
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?
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.
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.
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.
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.
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的有什么好处和坏处?
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的有什么好处和坏处?
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.
: 是好处,或者坏处就见仁见智了。
"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.
: 是好处,或者坏处就见仁见智了。
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的
【在 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的
相关阅读
请问化妆品里的cetearyl alcohol是做什么的你们一般用什么软件来画流程图之类的look for co-work do some chemical business in USARA转硕士的后果有哪些?peptide的分离有什么好的textbook推荐阅读一下请教一个问题can anyone tell me where I could find the price of methanol (转载)[help] Rhodamine (R6G) 在溶液中带正电还是负电在哪里能买到这种GC上用的solenoid valve啊?急问,关于光散射!做模拟的有可能去wallstreet么郁闷,找不到一个可以下载的好贺卡[合集] 女孩子适合学化工吗?Anybody knows the fuel cells group in University of South Carolina?求paper化工博士or硕士,找faculty或者找工作,什么比较重要?ASCII高手帮忙作个进版画面吧太牛了,一出大片啊斑斑请别删有没有做高分子微观机械性能建模,模拟的? (转载)