Should I ever synchronize for visibility of updates?
Up until now, I knew that if two threads use the same variable and one of them updates it, the other thread might not see the updated value for an extended period of time. Therefore there is the need to synchronize the access to this variable OR make the variable volatile (visibility of updates)
Trying to reproduce this behaviour, I've created the following piece of code where thread2 reads a variable which belongs to (and is updated by) thread1. No synchronized blocks nor volatiles are used:
publicclass Main
{
public Main()
{}
publicstaticvoid main(String[] args)
{
Thread1 th1 =new Thread1();
Thread2 th2 =new Thread2(th1);
th1.start();
th2.start();
}
}
publicclass Thread1extends Thread
{
publicint index;
public Thread1()
{
index=0;
}
publicvoid run()
{
while(true)
{
index++;
System.out.println("Thread 1: Increasing Index to " + index);
}
}
}
publicclass Thread2extends Thread
{
private Thread1 m_Th1;
public Thread2(Thread1 th1)
{
m_Th1 = th1;
}
publicvoid run()
{
while(true)
{
System.out.println("Thread 2: Index = " + m_Th1.index);
}
}
}
Here are the results I've got on an Intel Pentium Core2 1.8 Pentium using Java SE 1.5 and Netbeans 5.5:
Thread 1: Increasing Index to 1
Thread 2: Index = 1
Thread 1: Increasing Index to 2
Thread 1: Increasing Index to 3
Thread 1: Increasing Index to 4
Thread 1: Increasing Index to 5
Thread 1: Increasing Index to 6
Thread 1: Increasing Index to 7
Thread 1: Increasing Index to 8
Thread 1: Increasing Index to 9
Thread 1: Increasing Index to 10
Thread 1: Increasing Index to 11
Thread 1: Increasing Index to 12
Thread 1: Increasing Index to 13
Thread 1: Increasing Index to 14
Thread 1: Increasing Index to 15
Thread 1: Increasing Index to 16
Thread 1: Increasing Index to 17
Thread 1: Increasing Index to 18
Thread 1: Increasing Index to 19
Thread 1: Increasing Index to 20
Thread 1: Increasing Index to 21
Thread 1: Increasing Index to 22
Thread 1: Increasing Index to 23
Thread 1: Increasing Index to 24
Thread 1: Increasing Index to 25
Thread 1: Increasing Index to 26
Thread 2: Index = 26
Thread 1: Increasing Index to 27
Thread 2: Index = 27
Thread 1: Increasing Index to 28
Thread 2: Index = 28
Thread 1: Increasing Index to 29
Thread 2: Index = 29
Thread 1: Increasing Index to 30
Thread 2: Index = 30
Thread 1: Increasing Index to 31
Thread 1: Increasing Index to 32
Thread 1: Increasing Index to 33
Thread 1: Increasing Index to 34
Thread 1: Increasing Index to 35
Thread 1: Increasing Index to 36
Thread 1: Increasing Index to 37
Thread 2: Index = 37
Thread 1: Increasing Index to 38
Thread 2: Index = 38
Thread 1: Increasing Index to 39
Thread 2: Index = 39
Thread 1: Increasing Index to 40
Thread 2: Index = 40
Thread 1: Increasing Index to 41
Thread 2: Index = 41
Thread 1: Increasing Index to 42
Thread 2: Index = 42
Thread 1: Increasing Index to 43
Thread 2: Index = 43
Thread 1: Increasing Index to 44
Thread 2: Index = 44
Thread 1: Increasing Index to 45
Thread 2: Index = 45
Thread 1: Increasing Index to 46
Thread 2: Index = 46
Thread 1: Increasing Index to 47
Thread 2: Index = 47
Thread 1: Increasing Index to 48
Thread 2: Index = 48
Thread 1: Increasing Index to 49
Thread 2: Index = 49
Thread 1: Increasing Index to 50
Thread 2: Index = 50
Thread 2: Index = 50
Thread 2: Index = 50
It seems that the variable that thread 2 sees is ALWAYS up to date.
If this is true, then why should I synchronize in this type of case (visibility of updates)?
Could anybody tell me any architecture or configuration where I can see a different output value between thread 1 and 2?
Thanks in advance
George

