Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic