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

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

java - static synchronized and non static synchronized methods in threads

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

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

1 Answer

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...