class MainClass implements Runnable {
Object firstResource;
Object secondResource;
public MainClass(Object first, Object second) {
firstResource = first;
secondResource = second;
}
public void run() {
while (true) {
synchronized (firstResource) {
synchronized (secondResource) {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
}
}
}
}
public static void main(String[] args) {
Object a = "Resource A";
Object b = "Resource B";
Thread t1 = new Thread(new MainClass(a, b));
Thread t2 = new Thread(new MainClass(b, a));
t1.start();
t2.start();
}
}