• 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

Synchronized Block and Synchronized Method

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again,

Is is still considered good practice to have synchronized methods as well as some methods just standard with a synchronized block in the same class?

Personally I think it is alright, but would like to bounce it off you guys.

Thanks,
Pieter
 
Ranch Hand
Posts: 221
Scala Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pieter,


The simpler the code the easier it will be for a junior developer to maintain your code.
Since a high performance solution is not a MUST requirement, the use of synchronized methods is OK.

Just make sure you mention it in your choices.txt file.

Hope this helps.


Best,


Carlos.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pieter,

I think there is no harm in mixing synchronized methods with synchronized blocks. I didn't do it, because (like Carlos also mentioned) I took the as simple as possible approach (and performance wasn't a must requirement).

Kind regards,
Roel
 
Pieter Jacobs
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Carlos & Roel,

Thanks for the replies!! So you guys also marked your public methods as synchronized and left the private methods standard (unsynchronized)?

Thanks,
Pieter
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pieter,

That's indeed exactly what I did, because every public method is synchronized, every private method is used from a synchronized context and there is no need to mark them synchronized as well (but you just might do it).
If you use synchronized blocks, you have to mark private methods synchronized if you want that just one thread can access this method at a time (and other threads need to wait until that thread finishes the method).

Kind regards,
Roel
 
Pieter Jacobs
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roel,

Mega thanks again!

Enjoy the week,
Pieter
 
The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic