avatar
basic java question# Java - 爪哇娇娃
w*t
1
questions are:
if a java class has static block
static {
... (line 1)
...
... (line n)
}
I understand it's only executed only once when this class loaded first time.
what if 2 thread try to load this class at the same time, race condition
happens?
thanks,
avatar
st
2
I think so... you can make it singleton so it only gets load once.

【在 w******t 的大作中提到】
: questions are:
: if a java class has static block
: static {
: ... (line 1)
: ...
: ... (line n)
: }
: I understand it's only executed only once when this class loaded first time.
: what if 2 thread try to load this class at the same time, race condition
: happens?

avatar
w*t
3
actually my question comes from the singleton pattern I am using.
in my singleton class (pretty standard),
public class myclass {
private static myclass instance = new myclass();
... other variables ...
private myclass() {
....
}
public myclass getInstance() {
}
...
}
question is that: is it possible first line get executed more than once??
and should we use sync key word within constructor?
I use singleton many times and never think about it carefully, but suddenly I
have thi

【在 st 的大作中提到】
: I think so... you can make it singleton so it only gets load once.
avatar
e*g
4
http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#44630

【在 w******t 的大作中提到】
: actually my question comes from the singleton pattern I am using.
: in my singleton class (pretty standard),
: public class myclass {
: private static myclass instance = new myclass();
: ... other variables ...
: private myclass() {
: ....
: }
: public myclass getInstance() {
: }

avatar
m*c
6

No (I assume that the first line you mentioned is "private static myclass
instance = new myclass();"). The class loader will load your class before any
thread can access it.
Should "public myclass getInstance()" be "public static myclass getInstace()"?
If not, no one can create myclass since it only has private constructor.
In your case, probably not. Sometimes, people also use "Lazy Load" in their
singleton pattern, in that case, you may need to do it:
public myclass {
private static myc

【在 w******t 的大作中提到】
: actually my question comes from the singleton pattern I am using.
: in my singleton class (pretty standard),
: public class myclass {
: private static myclass instance = new myclass();
: ... other variables ...
: private myclass() {
: ....
: }
: public myclass getInstance() {
: }

avatar
e*g
7
it's unlikely that a singleton class is referenced, but instance is not needed
so lazy loading or not are the same in most cases.

【在 m**c 的大作中提到】
:
: No (I assume that the first line you mentioned is "private static myclass
: instance = new myclass();"). The class loader will load your class before any
: thread can access it.
: Should "public myclass getInstance()" be "public static myclass getInstace()"?
: If not, no one can create myclass since it only has private constructor.
: In your case, probably not. Sometimes, people also use "Lazy Load" in their
: singleton pattern, in that case, you may need to do it:
: public myclass {
: private static myc

avatar
w*t
8
Thanks for embug's link - it pretty much answers all my questions regarding
initialization.
mkac - good catch regarding getInstance() - it was a mistake while wrote the
post, not in my code - I know very well about singleton itself
but all your guys posts are very useful. Thanks

needed
once??
any
getInstace()"?
their
want
necessary.
I
to
suddenly

【在 e***g 的大作中提到】
: it's unlikely that a singleton class is referenced, but instance is not needed
: so lazy loading or not are the same in most cases.

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