w******t 发帖数: 1422 | 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, |
st 发帖数: 1685 | 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?
|
w******t 发帖数: 1422 | 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.
|
e***g 发帖数: 158 | 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() { : }
|
st 发帖数: 1685 | 5 also, I suggest wildwest read sun java singleton document. :D
【在 e***g 的大作中提到】 : http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#44630
|
m**c 发帖数: 90 | 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() { : }
|
e***g 发帖数: 158 | 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
|
w******t 发帖数: 1422 | 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.
|