xxxxxxxxxx
Student s = new Student();
synchronized (s){
}
class Student{
public static void staticMethod1(){
//can only be executed by the thread having the lock of class Student
synchronized(Student.class){
}
}
public static void staticMethod2(){ //can be executed by any thread
}
public void method1(){
//can only be executed by any thread
synchronized (this){
}
}
synchronized public void method2(){ //can only be executed by any thread
}
public void method3(){ //can only be executed by any thread
}
}
Synchronized blocks are applicable only for objects. If we try to use
synchronized blocks for primitives we get compile time error.