简体   繁体   中英

Are class variables in Java shared between threads or not?

I was wondering when declaring a class variable (ie a variable declared outside a method) would potentially cause problems in a program that's executed by several threads. I suppose final s are save to use (ie not shared), and static variables are definitely shared between threads. But what about "standard" variables? When are they shared and when are they "private" to each individual thread?

Update: While I accept that local variables (within methods) are not shared and class variables usually are I would love to understand why that is (from a technical point of view). So any explanation to that effect (or links to articles that are fairly easy to understand) would be much appreciated.

Java provides the ThreadLocal<T> class to declare variables that are not shared between threads. Non- final parameters and local variables are also not shared between threads. final parameters and locals variables may be shared between threads when they are used in an anonymous class definition, but this shouldn't be a problem since they are final . In all other cases I can think of, variables can be shared between threads.

The short answer is, any method or variable, both static and non- static , has the potential to be accessed by more than one Thread if its access modifier makes it visible to that Thread .

The concept of "thread-safe" is entirely different. If a variable is read-only ( final can be used to make primitives read-only, but only makes references to objects immutable, not the objects themselves), multi-threaded access of that variable is inherently thread-safe. If it can be written to, the proper use of synchronized blocks or the volatile keyword is necessary to ensure mutual exclusion.

Final doesn't mean 'not shared' just that the field cannot be overwritten, it can only be initialized one. Static behavior is also not related to threading, static means that the field has a Class scope and not instance scope, meaning it's shared by all the instances of a Class. If you want to protect a field from being modified by many threads, you have to manipulate it through synchronized methods.

这些变量被称为类的字段 ,它们通常不是 “线程安全的”,即使它们是final (因为final只引用引用本身,而不是对象内部的内容)。

There's nothing special about a standard variable that shields it from multi-threaded access and resulting problems. That's up to YOU the programmer to worry about.

If two threads need to safely access the same instance fields, then YOU have to write code to manage that.

Some common techniques are to use synchronized blocks to manage mutability, using atomic or final primitives (a final Object is not necessarily safe unless you know the Object isn't mutable), or simply use a ThreadLocal to give each thread its own unshared instance of the class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM