Confusion about synchronized methods
I wrote the code below to check class locks. It clarified my confusion about object locks and class locks.
class LockCheck
{
privateint id;
private String name;
privatestaticboolean staticChecked;
public LockCheck(int id, String name)
{
this.id = id;
this.name = name;
System.out.println(this +" object of LockClass created");
}
//Getters
publicint getId(){return id;}
public String getName(){return name;}
publicboolean isStaticChecked(){return staticChecked;}
//Setters
publicvoid setStaticChecked(boolean staticChecked){ this.staticChecked = staticChecked;}
//Function to lock class and hold it for 10 seconds.
publicstaticsynchronizedvoid lockClass()
{
System.out.println("class lock activated");
try
{
System.out.println(Thread.currentThread().getName() +" Thread sleeping for 10 sec.");
Thread.sleep(10000);
System.out.println(Thread.currentThread().getName() +" Thread woke up.");
}
catch (InterruptedException e)
{
System.out.println("Problem while Main Thread tried to sleep: " + e);
}
System.out.println("class lock released");
}
//Function to check class level member's lock
publicstaticvoid checkLockedClass()
{
System.out.println("Class Lock Checked: Class is accessable now");
}
//Function to check instance level member's lock
publicvoid checkObjectOfLockedClass()
{
System.out.println("Object Lock Checked: " + this.getName() +"(" + this.getId() +") object is accessable now");
}
//Normal Static method
publicstaticvoid staticMethod()
{
System.out.println("This Static Method is accessable now");
}
//override toString function.
public String toString(){return (this.getName() +"(" + this.getId() +")");}
};
class NewThreadimplements Runnable
{
private Thread lockThread;
private LockCheck lockCheck;
public NewThread(String threadName, LockCheck lockCheck)
{
this.lockCheck = lockCheck;
lockThread =new Thread(this, threadName);
System.out.println("Thread Created: " + threadName +" with object - " + lockCheck);
lockThread.start();
}
publicvoid run()
{
try
{
System.out.println(lockThread.getName() +" Thread sleeping for 2 sec.");
Thread.sleep(2000);
System.out.println(lockThread.getName() +" Thread woke up.");
}
catch (InterruptedException e)
{
System.out.println("Problem while " + lockThread.getName() +" Thread tried to sleep: " + e);
}
lockCheck.checkObjectOfLockedClass();
if(!lockCheck.isStaticChecked())
{
lockCheck.checkLockedClass();
lockCheck.setStaticChecked(true);
LockCheck.staticMethod();
//Synchronized method to be called here
}
System.out.println(lockThread.getName() +" Thread with object - " + lockCheck +" Exiting...");
}
};
publicclass ClassLockTest
{
publicstaticvoid main(String[] args)
{
//create two LockCheck objects.
LockCheck lc1 =new LockCheck(1234,"FirstLockCheck");
LockCheck lc2 =new LockCheck(5678,"SecondLockCheck");
//Create two separate threads for created objects.
NewThread t1 =new NewThread("FirstThread", lc1);
NewThread t2 =new NewThread("SecondThread", lc2);
//lock class for 10 seconds.
LockCheck.lockClass();
System.out.println("Main Thread Exiting...");
}
};
the output of this program is:
- Run -
FirstLockCheck(1234) object of LockClass created
SecondLockCheck(5678) object of LockClass created
Thread Created: FirstThread with object - FirstLockCheck(1234)
Thread Created: SecondThread with object - SecondLockCheck(5678)
class lock activated
main Thread sleeping for 10 sec.
FirstThread Thread sleeping for 2 sec.
SecondThread Thread sleeping for 2 sec.
FirstThread Thread woke up.
Object Lock Checked: FirstLockCheck(1234) object is accessable now
Class Lock Checked: Class is accessable now
This Static Method is accessable now
FirstThread Thread with object - FirstLockCheck(1234) Exiting...
SecondThread Thread woke up.
Object Lock Checked: SecondLockCheck(5678) object is accessable now
SecondThread Thread with object - SecondLockCheck(5678) Exiting...
main Thread woke up.
class lock released
Main Thread Exiting...
Output completed (10 sec consumed) - Normal Termination
But when we replace the line
//Synchronized method to be called here
By
LockCheck.lockClass();
then the output is:
- Run -
FirstLockCheck(1234) object of LockClass created
SecondLockCheck(5678) object of LockClass created
Thread Created: FirstThread with object - FirstLockCheck(1234)
Thread Created: SecondThread with object - SecondLockCheck(5678)
class lock activated
main Thread sleeping for 10 sec.
FirstThread Thread sleeping for 2 sec.
SecondThread Thread sleeping for 2 sec.
FirstThread Thread woke up.
Object Lock Checked: FirstLockCheck(1234) object is accessable now
Class Lock Checked: Class is accessable now
This Static Method is accessable now
SecondThread Thread woke up.
Object Lock Checked: SecondLockCheck(5678) object is accessable now
SecondThread Thread with object - SecondLockCheck(5678) Exiting...
main Thread woke up.
class lock released
class lock activated
FirstThread Thread sleeping for 10 sec.
Main Thread Exiting...
FirstThread Thread woke up.
class lock released
FirstThread Thread with object - FirstLockCheck(1234) Exiting...
Output completed (20 sec consumed) - Normal Termination
So I concluded from here, that there is no class lock, there is only resource lock.
And resources of a class are its object and its static methods, as they are having their separate existance.(although they are linked together).
That means, Objects are having their own separate monitors,
and static methods are having their own separate monitors.
Am I right?
And when we access something in a synchronized block, for eg some third party class, then, if synchronized block is in an instance method then that object's monitor is locked.
and if synchronized block is in a static method, then that static method's monitor is locked.
Am I right? -> or the third partiy's created object's monitor is locked.
Thanks in advance for any guidance.

