is this ENUM code thread safe?
enum One{
IONE;
public mone(){
// some code
}
}
when above code is not thrad safe?
is this ENUM code thread safe?
enum One{
IONE;
public mone(){
// some code
}
}
when above code is not thrad safe?
An enum value is guaranteed to only be initialized once, ever, by a single thread, before it is used.However, methods that you add to an enum class do not carry any thread safety guarantee. If you have an enum so that the methods don't change its state, then they are by definition thread safe
It is, as long as your method itself is.
Enums are treated specially:
final
;static final
.There are no shared data in the One
enum, and all the variables in mone
(which needs a return type) are local ones. So, it all depends on the content of mone
. If the method uses singletons with state in other classes, you can have problems.