• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
K Mudof
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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,
k
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
K Mudof
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".

k
 
Michael Clare
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic