• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Synchronization - Static methods and Blocks

 
Ranch Hand
Posts: 45
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Static methods can be synchronized.
Does it means that the static data (which is the single copy for the entire class) is blocked?


synchronized(MyClass.class){

// some synchronized stuff
}

In the above code, static data of MyClass is locked?

Please let me know if i am on the wrong front.

Thanks.

Regards
Senthil Kumar Sekar
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Senthil Kumar Sekar wrote:In the above code, static data of MyClass is locked?


No. In given code, only code which lies within synchronized block is locked.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might also want to read Java Concurrency tutorial.
 
Senthil Kumar Sekar
Ranch Hand
Posts: 45
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok then, Code in both the below blocks will be executed not more than one thread at a time.




What does someObject & MyClass.class in synchronized block indicate?

 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first code snippet, code is synchronized on object referred by someObject. So, if two(or more) threads are having same reference someObject, then only one thread at a time can enter in synchronized block.

In the second code snippet, code is synchronized on object referred by MyClass.class.

If you have any doubts regarding synchronization, I would suggest to go through link provided in previous post by Martin Vajsar.
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic