I try on my intelij and everything run fine. But i think that maybe something went wrong. atleast first two requirement. It's weird. I mean why it can return true and false on same condition.
package com.codegym.task.task27.task2707;
/*
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 {
//do something here
final String[] s = {null, null};
new Thread(new Runnable() {
@Override
public void run() {
synchronized (o2){
try{
Thread.sleep(100);
}catch (Exception e){
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
solution.someMethodWithSynchronizedBlocks(o1,o2);
s[0] = "hau";
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
synchronized (o1){
s[1] = "hau";
try{
Thread.sleep(100);
}catch (Exception e){
}
}
}
}).start();
while (s[1] == null){
Thread.sleep(1);
}
if(s[0] != null) {
return true;
}
return false;
}
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));
}
}