Question: ```
import java.util.ArrayList;

public class ThreadTest {

    public static void main(String[] args) {

        ArrayList arr = new ArrayList();        
        for (int  i =0; i<10 ; i++)
            arr.add(new MyTh(i));   
        for (int  i =0; i<10 ; i++) {
            ((Thread ) arr.get(i)).start();
        }
    }

}

class MyTh extends Thread {

   int num;

   volatile int  age;

   MyTh (int  k)

   {this.num=k; } 
   @Override
   public void run() {
    if(this.num==4){
        setAge(40);
    }
    System.out.println(Thread.currentThread().getName() + " :"+this.num + ":" + getAge());
}
    synchronized int getAge() {
       return age;
    }
    synchronized void  setAge(int  k) {
        this.age =k;
    }
}

```

I am puzzled here why the output of the program is printing value of volatile variable age = 40 only once .
I tried keeping synchronized in getter / setter both , then too only single thread 4th prints it 40 . Why not other subsequent thread read it 40 too ?

```
Thread-0 :0:0
Thread-1 :1:0
Thread-2 :2:0
Thread-3 :3:0
Thread-4 :4:40
Thread-5 :5:0
Thread-6 :6:0
Thread-7 :7:0
Thread-8 :8:0
Thread-9 :9:0

```

Answer: 
Why you use `synchronized` keyword for getters and setters while your variable is declared `volatile`. It is not needed here. You just need the visibility, not atomicity. So change your code like so,

```
public class MyTh extends Thread {
    int num;

    static volatile int age;

    MyTh(int k)

    {
        this.num = k;
    }

    @Override
    public void run() {
        if (this.num == 4) {
            setAge(40);
        }
        System.out.println(Thread.currentThread().getName() + " :" + this.num + ":" + getAge());
    }

    int getAge() {
        return age;
    }

    void setAge(int k) {
        age = k;
    }
}

```

First mark the `age` variable as `static` so that the same is shared among all the instances of the class. In your previous case it was an instance field, hence each instance had it's own copy. Hence change made by one is not visible to the others.

Latest output is given below.

```
Thread-0 :0:0
Thread-3 :3:0
Thread-2 :2:0
Thread-1 :1:0
Thread-4 :4:40
Thread-6 :6:40
Thread-8 :8:40
Thread-5 :5:40
Thread-7 :7:40
Thread-9 :9:40

```
There are two things here :

1. You are using a instance variable **age** and creating a 10 instances of MyTh class so each instance will have its own copy and they will not interfere with each others operation. So as per the suggestion from @tanyehzheng you will have to make it static so that it can be shared across all the 10 instances.
2. Your question about **What difference volatile will make if we have static** :

	* **static** only states that the copy will be shared across all the instances and it doesn't guarantee memory visibility in case of multiple threads trying to update/access it.
	* **volatile** on the other hand will guarantee Memory visibility, i.e. if any write has occurred to the variable then all the subsequent read will see the latest value (happens-before relation). Which was not possible if you use only static because CPU core may cache the value or have it in register.