Synchronized methods and virtual threads

Cay Horstmann cay.horstmann at gmail.com
Mon Jun 3 07:20:59 UTC 2024


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


More information about the loom-dev mailing list