Synchronized methods and virtual threads
robert engels
robaho at icloud.com
Mon Jun 3 13:26:40 UTC 2024
They are one and the same when they lock on the object itself. Synchronized block statements allow the lock duration to be minimized while maintaining the natural code structure (I.e not adding additional methods).
> On Jun 3, 2024, at 8:24 AM, Cay Horstmann <cay at horstmann.com> wrote:
>
> I am not talking about synchronized methods but about block statements (JLS 14.19).
>
> synchronized (obj) {
>
> }
>
>> On 03/06/2024 14.31, robert engels wrote:
>> Reference for synchronized being a code smell?
>> Synchronized methods can be much more efficient than locks as the runtime can optimize them away, bias locking, etc.
>>>> On Jun 3, 2024, at 2:21 AM, Cay Horstmann <cay.horstmann at gmail.com> wrote:
>>>
>>> On 02/06/2024 13.07, Andrew Haley wrote:
>>>>> On 6/1/24 17:56, Alex wrote:
>>>>> Before this fix many projects did fixes and replaced synchronized with Lock. What is the latest recommendation? Keep replacing synchronized with Lock or wait for Java 23 and use synchronized methods?
>>>> I'd use Lock for the future. Synchronized plays very badly with Value
>>>> Objects, for example. Being able to synchronize on any object is something
>>>> of a legacy feature.
>>>
>>> At least since Java 5, synchronized(obj) has been a code smell. A class with synchronized methods can be appropriate when carefully designed. (Of course, value classes cannot have synchronized methods.)
>>>
>>> --
>>>
>>> Cay S. Horstmann | https://horstmann.com
>
> --
>
> --
>
> Cay S. Horstmann | https://horstmann.com
More information about the loom-dev
mailing list