| Author |
Private static variables in a static class
|
Mike London
Ranch Hand
Joined: Jul 12, 2002
Posts: 951
|
|
Are private static variables inside a static method thread safe? I'm assuming that private static variables at the class level are not since they're properties of the class. Look forward to any reply. Thanks. M
|
 |
Leandro Melo
Ranch Hand
Joined: Mar 27, 2004
Posts: 401
|
|
Hi. Since the variable is private, only its class is allowed to modify it. If you have only one static method that manipulates this variable I'd say yes, it's thread-safe, because this variable is not accessible through other means. However, if you have other static methods manipulating this same variable, things change and you might need to handle multi-threading issues.
|
Leandro Melo <br />SCJP 1.4, SCWCD 1.4<br /><a href="http://www.pazbrasil.org/" target="_blank" rel="nofollow">http://www.pazbrasil.org/</a>
|
 |
Mike London
Ranch Hand
Joined: Jul 12, 2002
Posts: 951
|
|
Thanks...wouldn't the best solution be to just include the class' static private static variables inside the static method that uses them? That way, if you needed a similar variable, there wouldn't be a temptation to reuse it from another static method in the same class. What do you think? M
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
Sorry, but that is not correct. Suppose that private static variable is accessed by a public static method. Then it is possible for two threads to call that method at the same time. That situation is not thread-safe unless the method is synchronized. It makes no difference whether the method or the variable are static or not.
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
Adding a little more... it also makes no difference whether there's one method, or two. What's important is whether or not more than one thread can access the method or methods. If so, then the method(s) must be written in a thread-safe manner, using synchronization or other techniques. [Mike]: Thanks...wouldn't the best solution be to just include the class' static private static variables inside the static method that uses them? It depends on what you mean by "include". If you make the variable a local variable inside the method, instead of a class or instance variable, then yes, that will work great. But if you use any non-final instance or class variable, at all, then you need to take additional steps (usually, synchronization) to make it thread-safe. The other techniques are more complicated to figure out how to use safely. They include using volatile or atomic data types, or possibly ThreadLocal or other ways to restrict thread access to mutable data. The details are best discussed by a book such as Java Concurrency in Practice or Java Threads. [ June 01, 2007: Message edited by: Jim Yingst ]
|
"I'm not back." - Bill Harding, Twister
|
 |
Mike London
Ranch Hand
Joined: Jul 12, 2002
Posts: 951
|
|
Thanks all for the great replies. I'll move the class-level private static variables inside the static methods and call it day!!! Thanks again. M
|
 |
 |
|
|
subject: Private static variables in a static class
|
|
|