Suppose I have two threads updating an object, and one thread reading from that object with no synchronization. Obviously, this is run condition. However, I am wondering if the variable itself can only partially written.
public class CommonObject extends Object
{
static int memberVar=-1;
}
public class Input1Thread extends Thread
{
public void run()
{
while(true)
CommonObject.memberVar = 1
}
}
public class Input2Thread extends Thread
{
public void run()
{
while(true)
CommonObject.memberVar = 2;
}
}
public class OutputThread extends Thread
{
public void run()
{
while(true)
System.out.println("CommonObject.memberVar"+ CommonObject.memberVar);
}
}
I would assume the value printed out will also be either 2 or 1. However, I was wondering if it was possible that the variable might be halfway set?
I used primitives as an example, but I would like the answer would for objects also, if it is different.