Forums Register Login

Synchronized Block and Synchronized Method

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Hi Roel,

Mega thanks again!

Enjoy the week,
Pieter
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 850 times.
Similar Threads
Synchronized block scope
static synchronized block
servlet and thread synchronization
Threads
Synchronized block and methods
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:14:49.