I can't say I fully understand why this could be required, but using wrapper approach may potentially help:
public class SyncOnEquals {
public synchronized Object getSync(Object o1, Object o2)
{
if(o1.equals(o2))
return this;
else
return o1;
}
}
Test:
@org.junit.Test
public void testSyncOnEqual() {
System.out.println("syncOnEqual");
Integer o1 = new Integer(125);
Integer o2 = new Integer(125);
System.out.println("o1 == o2: "+(o1==o2));
System.out.println("o1.equals(o2): "+o1.equals(o2));
SyncOnEquals sync = new SyncOnEquals();
Thread t1 = new Thread(){
public void run()
{
System.out.println("Waiting thread "+Thread.currentThread());
synchronized(sync.getSync(o1, o2))
{
System.out.println("Working thread "+Thread.currentThread());
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
System.out.println("Finished thread "+Thread.currentThread());
}
};
Thread t2 = new Thread(){
public void run()
{
System.out.println("Waiting thread "+Thread.currentThread());
synchronized(sync.getSync(o2, o1))
{
System.out.println("Working thread "+Thread.currentThread());
try {
Thread.currentThread().sleep(500);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
System.out.println("Finished thread "+Thread.currentThread());
}
};
t1.start();
t2.start();
try{
Thread.currentThread().sleep(2000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
Output:
syncOnEqual
o1 == o2: false
o1.equals(o2): true
Waiting thread Thread[Thread-0,5,main]
Waiting thread Thread[Thread-1,5,main]
Working thread Thread[Thread-0,5,main]
Finished thread Thread[Thread-0,5,main]
Working thread Thread[Thread-1,5,main]
Finished thread Thread[Thread-1,5,main]