Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

can any one explain the statement ..."static synchronized method and non static synchronized method will not block each other -they can run at the same time"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
359 views
Welcome To Ask or Share your Answers For Others

1 Answer

static synchronized void test() { foo(); }

equals

static void test() { synchronized(MyClass.class) { foo(); } }

while

synchronized void test() { foo(); }

equals

void test() { synchronized(this) { foo(); } }

This means: static methods lock on the class object of the class. Non-static methods lock on the instance on which they're called (by default, synchronized(anyOtherLock) is also possible). Since they lock on different objects, they don't block each other.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...