Sub topic 2
What Makes State Shared and Mutable
In the last chapter, you saw that threads can run code at the same time. Most of the time, that causes no trouble. Trouble starts only when threads touch the same data and that data can change. This chapter shows you how to tell when that is the case.
What "state" means
State is the data a program holds at a given moment. In Java, state lives in variables: local variables, fields on objects, and static fields.
State has two separate properties that matter for threads:
- Shared means more than one thread can reach the same variable.
- Mutable means the value can change after it is first set.
Thread safety problems need both properties together. If data is shared but never changes, or changes but belongs to one thread, it is safe.
Which variables are shared?
Each thread gets its own stack. A stack is a private memory area that holds a method's local variables. So a local variable inside a method is never shared, even when many threads run the same method.
Fields are different. An object lives on the heap, which is memory that all threads can reach. If two threads hold a reference to the same object, they share its fields. A static field belongs to the class itself, so every thread shares it.
public class SharingDemo {
// Static field: one copy, shared by every thread.
private static final String GREETING = "Hello";
// Instance field: shared by every thread that uses the same object.
private int visits = 0;
public void visit() {
// Local variable: each thread gets its own copy.
int steps = 0;
for (int i = 0; i < 3; i++) {
steps++;
}
System.out.println(Thread.currentThread().getName()
+ " " + GREETING + ", steps = " + steps);
}
public static void main(String[] args) throws InterruptedException {
SharingDemo demo = new SharingDemo();
Thread t1 = new Thread(demo::visit, "thread-1");
Thread t2 = new Thread(demo::visit, "thread-2");
t1.start();
t2.start();
t1.join();
t2.join();
}
}Each thread always prints steps = 3, because steps is local. GREETING is shared but final, so it can never change. The field visits is the risky one. Both threads use the same demo object, so if visit() changed visits, both threads would be changing one variable.
A quick sorting table
| Kind of variable | Shared? | Needs protection? |
|---|---|---|
| Local variable (like `steps`) | No | No |
| `final` field holding a value that cannot change | Yes | No |
| Non-final field on an object used by many threads | Yes | Yes |
| Non-final static field | Yes | Yes |
Asking two questions
You can check any piece of data with two questions.
Later chapters show the two ways out. You can protect shared mutable state with tools like synchronized, or you can design it away by making data unchangeable or by keeping it inside one thread.
Exercise
A method declares List<String> names = sharedRegistry.getNames(); and then adds an item to names. Two threads call this method on the same sharedRegistry. Is the data shared and mutable?
Show answer
Yes. The local variable names is private to each thread, but both copies point to the same list object on the heap. Adding an item changes that list, so the list is shared and mutable and needs protection.
Exercise
Which variable in SharingDemo would become unsafe if visit() ran visits++, and why?
Show answer
The field visits. Both threads use the same demo object, so they share the field, and visits++ changes its value.