Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Synchronized Block and Synchronized Method

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Hi Roel,

Mega thanks again!

Enjoy the week,
Pieter
 
I once met a man from Nantucket. He had a tiny ad
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic