So according to the requirements, the method must return both true and false.
When i uncomment one of the return statements and comment the other, i pass or the first, or the second requirement, but not both. That's annoying, my solution is working. Thanks :)
package com.codegym.task.task27.task2707;
import java.util.concurrent.atomic.AtomicReference;
/*
Determining locking order
*/
public class Solution {
public void someMethodWithSynchronizedBlocks(Object obj1, Object obj2) {
synchronized (obj1) {
synchronized (obj2) {
System.out.println(obj1 + " " + obj2);
}
}
}
public static boolean isLockOrderNormal(final Solution solution, final Object o1, final Object o2) throws Exception {
AtomicReference<String> string = new AtomicReference<>();
Thread thread = new Thread(() -> {
synchronized (o1) {
string.set("o1");
}
});
Thread thread2 = new Thread(() -> {
synchronized (o2) {
string.set("o2");
}
});
solution.someMethodWithSynchronizedBlocks(o1, o2);
thread.start();
thread2.start();
Thread.sleep(200);
return !string.toString().equals("o2");
}
public static void main(String[] args) throws Exception {
final Solution solution = new Solution();
final Object o1 = new Object();
final Object o2 = new Object();
System.out.println(isLockOrderNormal(solution, o1, o2));
}
}