Interview Questions

Output question 21.

Java Interview Questions and Answers (part 4)


(Continued from previous question...)

81. Output question 21.

public class MyClass extends Thread{
public void run() {
method1();
}

public static void method1() {
synchronized (this) {
System.out.println("2 ");
}
}

public static void main(String[] args) {
new Thread(new MyClass()).start();
}

}


Answer.

We will face compilation error at line synchronized (this) can’t use in static context, because it’s not possible to obtain lock on object from static method. Though we can obtain lock on class’s class object , so synchronized (MyClass.class) will be a valid statement.

(Continued on next question...)

Other Interview Questions