Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads blocking on static method.... or do they? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads blocking on static method.... or do they?" Watch "Threads blocking on static method.... or do they?" New topic

Threads blocking on static method.... or do they?

K Mudof

Joined: May 09, 2010
Posts: 13

Hi everyone.... the first post Btw, congratulations for indeed a great forum!!!

My question is regarding K&B book, specificly about "threads calling static synchronized method in the same class will always block each other", page 739.

So I prepare a little piece of program below with expectation my computer will start to levitate when those threads will fight for my bueatyfull static method. Here is the code:

Unfortunatelly, when executing this little program, my computer does not leviate nor even JVM freezes. Instead I have rather boring console output:

T1 prints: 1 2 3 4 5 T2 prints: 1 2 3 4 5

My question is: why? Do I misunderstand what "blocking "means?

Thanks in advance,
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14669

Welcome to JavaRanch!

Why exactly did you expect this program to make your computer levitate or freeze?

What happens is that first the first thread runs, it grabs the lock, and when it's finished, it releases the lock and the second thread runs. The second thread has to wait on the lock because the first thread has locked it. Static synchronized methods synchronize on the class object of the class that they're in. So this:

is equivalent to this:

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
K Mudof

Joined: May 09, 2010
Posts: 13
Thanks Jesper. Yes, it works as you say. However K&B book suggests they will block each other becasue "they (both threads) lock on the same Class instance". So is it a mistake in the book or my misinterpretation of blocking each other meaning non of this threads will be able to execute the method, because both will wait for each other to grab and execute the method?

Ok, subquestion. How could I modify this code so both threads actually will block each other?

Thanks, k
K Mudof

Joined: May 09, 2010
Posts: 13
I think I understand my mistake. In K&B book blocking actually means getting a lock and holding it when till the method execution is finished. So thats a good thing, thats what we want. Where as in the next example on the same page quote is: "synchronized, satic and non-static method will not block each other, ever". And thats bad, becase it defeats our intention of synchronization.

My mistake was misinterpretation of a word "blocking". I had in mind "deadlock".

Michael Clare

Joined: Apr 03, 2010
Posts: 28
If they didn't block each other you might get something like T1: 1 2 T2: 1 T1: 3 T2: 2 3 T1: 4 5 T2: 4 5.
Which is not what you want in some cases.
wood burning stoves
subject: Threads blocking on static method.... or do they?