Is it possible to synchronize a variable such that all references to that variable are implicitly assumed to be synchronized? I'm thinking something like the following:
synchronized List l = new LinkedList(); // I know this isn't valid
l.add(4)
l.set(0, new Integer(5));
// add a bunch of things
l.get((int) (Math.random()*l.size()));
would be compiled as
List l = new LinkedList();
synchronized(l){
l.add(4)
}
Integer temp = new Integer(5);
synchronized(l){
l.set(0, temp);
}
// add a bunch of things
double temp2 = Math.random();
synchronized(l){
int temp3 = l.size()
}
int temp4 = (int) temp3;
synchronized(l){
l.get(temp4);
}
EDIT This is just an example using List. I'm looking for a general way to do this for any object I might create.