由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - basic java question
相关主题
thread safe Singleton 的几种方法?Where does Java Store Static Variable?
static getInstance()一个关于generics的问题
面试的一些题目anyone saw this on code?
what is your opinion in this case?我脑袋短路,大家来帮一下:
看到一个关于singleton的面试题Singleton
跪求大牛指点Java,看不懂什么意思。JAVA 考试题请教
question: how to implement thisstructure in Java??
Singleton vs. static class?如何关闭打开的输入输出?
相关话题的讨论汇总
话题: myclass话题: singleton话题: class话题: static话题: java
进入Java版参与讨论
1 (共1页)
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.

1 (共1页)
进入Java版参与讨论
相关主题
如何关闭打开的输入输出?看到一个关于singleton的面试题
NullPointerException 问题跪求大牛指点Java,看不懂什么意思。
问一个java基础的初始化的问题,一直搞不明白 (转载)question: how to implement this
HashMap entry lazy initialization in multithreaded enviroment?Singleton vs. static class?
thread safe Singleton 的几种方法?Where does Java Store Static Variable?
static getInstance()一个关于generics的问题
面试的一些题目anyone saw this on code?
what is your opinion in this case?我脑袋短路,大家来帮一下:
相关话题的讨论汇总
话题: myclass话题: singleton话题: class话题: static话题: java