This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Static method calls  RSS feed

 
Tim Williams
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a class has a static method, and that method is called by two independent threads, will the the two calls be processed concurrently?
My question arrises from performance considerations. If the JVM blocks one thread while the other runs then it would be better to use instance methods so blocking does not occur.
Furthermore, depending on the answer to the above, why is it possible to have synchronized static methods?
Thanks for your help,
Tim.
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim
I have written the following code to specify why static methods may be required to be synchronized

The output if you uncomment case 1 and comment case 2 are
With Synchronization (Case 1) Without synchronization (Case 2)
---------- java ---------- 1 second outside 2 second inside 3 first outside 4 first inside 5 second outside 6 second inside 7 first outside 8 first inside 9 second outside 10 second inside 11 first outside 12 first inside Normal Termination Output completed (32 sec consumed). ---------- java ---------- 1 second outside 2 first outside 3 second inside 4 second outside 5 first inside 6 first outside 7 second inside 8 second outside 9 first inside 10 first outside 11 second inside 12 first inside Normal Termination Output completed (17 sec consumed).

Basically a thread that enters the unsynchronized method setx expects it to get the value of x incremented by 1 after it sleeps for a period. However it gets an increment of 2 instead of 1 at the end of the method due to the existence of another thread.
Hope this helps else i will explain things for you

[This message has been edited by Rahul Mahindrakar (edited February 14, 2001).]
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If a class has a static method, and that method is called by two independent threads, will the the two calls be processed concurrently?

Ans : Yes it may be processed concurrently as the code above proves it

My question arrises from performance considerations. If the JVM blocks one thread while the other runs then it would be better to use instance methods so blocking does not occur.

Ans: You may now have to use synchronization on the instance methods to preserve Object state from concurrent access.


Furthermore, depending on the answer to the above, why is it possible to have synchronized static methods?

synchronization of static methods and provision of static blocks is a language feature java. It is upon us to use and implement it wisely.

------------------
Regds.
Mahindrakar
 
Tim Williams
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that! Just what I needed.
Tim.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!