Java Thread lock on static method
As per my knowledge in Java class with
Non static synchronize method : lock acquire on particular object
Static synchronize method : lock acquire on class
I am little bit confuse with this, since we can call the static method by
class name OR by object name.
please assume there are 4 methods is my class all are synchronize. 2
methods are static and 2 are non static. if i will create 1 object of my
class "obj1" and there are 2 thread Thread1 and Thread2 as well
Question 1 : if i will try to access the static synchronize method, using
the obj1 (or class name). does it mean there is no lock on "obj1" only 2
static methods will be locked (class level lock) ? means other thread can
access non static method but not the static method using the "obj1"
simultaneously ?
Question 2 : if i will try to access the non static synchronize method,
using the obj1 in Thread1. does it mean only 2 methods are locked for
Thread2 ? means Thread2 can access 2 static method , OR we can access the
static method using the className(MyClass) as well simultaneously
Question 3 : if all the methods in my class are static and synchronize .
does it means there will be no object level lock and there is only one
class level lock for all thread. ?
please explain little bit about the class level lock
Thanks in advance.
No comments:
Post a Comment