j.u.c.Lock with try-with-resources

Markus Knetschke markus.knetschke at gmail.com
Tue Jun 1 18:07:46 UTC 2021


Hi,

I like to open a discussion about TWR support for j.u.c.Lock.
There were already multiple threads about this but I think now with
Valhalla & Lilliput trying to get rid of the object monitor and Loom
also having problems with the monitor the incentive to use Lock is
higher than ever.
So I think it's again time to discuss the TWR friendliness, either
through a new Lock class (which would not be my preferred way) or the
extension of the Lock interface.

One example I could think of would be an addition of:
interface CloseableLock extends AutoCloseable {
   void close();
}

default CloseableLock autoLock() {
    lock();
    return this::unlock;
}

Implementations of this could avoid the allocation by implementing
CloseableLock and returning "this" instead.

This would take one pain point of not using object monitors with
synchronized blocks away and get rid of the missed unlocks I sometimes
see.

Best regards,
Markus Knetschke


More information about the discuss mailing list