On Thu, Jun 23, 2016 at 4:56 PM, Gary Gregory <[email protected]>
wrote:
> Like this:
>
> public static boolean hasManager(final String name) {
> try (Object o = AutoCloseableLock.lock(LOCK)) {
> return MAP.containsKey(name);
> }
> }
>
> With a new class:
>
> public class AutoCloseableLock implements AutoCloseable {
>
> public static AutoCloseableLock lock(final Lock lock) {
> Objects.requireNonNull(lock, "lock");
> lock.lock();
> return new AutoCloseableLock(lock);
> }
>
> private final Lock lock;
>
> public AutoCloseableLock(final Lock lock) {
> this.lock = lock;
> }
>
> @Override
> public void close() {
> this.lock.unlock();
> }
>
> public void lock() {
> this.lock.lock();
> }
>
You do not even need a lock() method.
Gary
> }
>
> The pro is less code and less chance for errors, the con is that you
> create a new AutoCloseableLock for lock/unlock sites.
>
> Gary
>
> On Thu, Jun 23, 2016 at 4:54 PM, Matt Sicker <[email protected]> wrote:
>
>> Sounds like an opportunity for a small Commons or independent project.
>> CloseableLock!
>>
>> On 23 June 2016 at 18:51, Greg Thomas <[email protected]> wrote:
>>
>>> I''m sure I read somewhere that it was a deliberate choice not to make
>>> it, to stop people using the very common pattern of creating the object in
>>> the try() - which isn't much use for a lock.
>>>
>>> Greg
>>> --
>>> Sent from my iPhone
>>>
>>> On 24 Jun 2016, at 00:45, Remko Popma <[email protected]> wrote:
>>>
>>> Good idea!
>>> Maybe propose this for Java 9?
>>> Looks very reasonable to me.
>>>
>>> Sent from my iPhone
>>>
>>> On 2016/06/24, at 8:32, Gary Gregory <[email protected]> wrote:
>>>
>>> I wonder if anyone knows why Lock is not AutoCloseable.
>>>
>>> This:
>>>
>>> public static boolean hasManager(final String name) {
>>> LOCK.lock();
>>> try {
>>> return MAP.containsKey(name);
>>> } finally {
>>> LOCK.unlock();
>>> }
>>> }
>>>
>>>
>>> Seems lame in comparison to:
>>>
>>> public static boolean hasManager(final String name) {
>>> try (LOCK.lock()) {
>>> return MAP.containsKey(name);
>>> }
>>> }
>>>
>>> Which, due to syntax really would be:
>>>
>>> public static boolean hasManager(final String name) {
>>> try (Object o = LOCK.lock()) {
>>> return MAP.containsKey(name);
>>> }
>>> }
>>>
>>> Just wonderin'...
>>>
>>> Gary
>>>
>>> --
>>> E-Mail: [email protected] | [email protected]
>>> Java Persistence with Hibernate, Second Edition
>>> <http://www.manning.com/bauer3/>
>>> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
>>> Spring Batch in Action <http://www.manning.com/templier/>
>>> Blog: http://garygregory.wordpress.com
>>> Home: http://garygregory.com/
>>> Tweet! http://twitter.com/GaryGregory
>>>
>>>
>>
>>
>> --
>> Matt Sicker <[email protected]>
>>
>
>
>
> --
> E-Mail: [email protected] | [email protected]
> Java Persistence with Hibernate, Second Edition
> <http://www.manning.com/bauer3/>
> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
> Spring Batch in Action <http://www.manning.com/templier/>
> Blog: http://garygregory.wordpress.com
> Home: http://garygregory.com/
> Tweet! http://twitter.com/GaryGregory
>
--
E-Mail: [email protected] | [email protected]
Java Persistence with Hibernate, Second Edition
<http://www.manning.com/bauer3/>
JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
Spring Batch in Action <http://www.manning.com/templier/>
Blog: http://garygregory.wordpress.com
Home: http://garygregory.com/
Tweet! http://twitter.com/GaryGregory