Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 0E9F0200B38 for ; Fri, 24 Jun 2016 03:48:10 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 0D0C1160A68; Fri, 24 Jun 2016 01:48:10 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id D50C5160A59 for ; Fri, 24 Jun 2016 03:48:08 +0200 (CEST) Received: (qmail 90056 invoked by uid 500); 24 Jun 2016 01:48:07 -0000 Mailing-List: contact log4j-dev-help@logging.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Log4J Developers List" Reply-To: "Log4J Developers List" Delivered-To: mailing list log4j-dev@logging.apache.org Received: (qmail 90046 invoked by uid 99); 24 Jun 2016 01:48:07 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 24 Jun 2016 01:48:07 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id E5F64C028E for ; Fri, 24 Jun 2016 01:48:06 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.198 X-Spam-Level: * X-Spam-Status: No, score=1.198 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HTML_MESSAGE=2, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_PASS=-0.001] autolearn=disabled Authentication-Results: spamd4-us-west.apache.org (amavisd-new); dkim=pass (2048-bit key) header.d=gmail.com Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id Da4aSZn2j3Rt for ; Fri, 24 Jun 2016 01:48:04 +0000 (UTC) Received: from mail-oi0-f43.google.com (mail-oi0-f43.google.com [209.85.218.43]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with ESMTPS id 91DF15F39B for ; Fri, 24 Jun 2016 01:48:03 +0000 (UTC) Received: by mail-oi0-f43.google.com with SMTP id f189so96701834oig.3 for ; Thu, 23 Jun 2016 18:48:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=LqiSnF21wkD1mwQUEqS6H3zY3dKSMjfAJKqDZ7NDwdI=; b=KbbjYZi3YGojJEAC0bISGU2DoIhYEIe3KAFMKYIZOWKefkfRiaw08txRpjkgCEGxrk mZKU0DlJQ2YiXJP7HqVp5Hf80blWmb4MsjH4EJox6oqfZ3AUg1Z+TkkwmRLs4AJqfq/2 pqBJw+QT/A8fXUJ15iXXkl2odvUHTcJkLJwJ8Fc3nVZogSlxyrIyikJTVcZvw0h36xPI Pm4TZLidoTlpCR4IK36lo7TNzBnnZjHvGmT6Ne1MRg8H+Rwt6V0NYyqarEBfqdmcbmcC RS8/d2Kj5pK4QB74bgOPwyB1nmdyQHrfznscA05cSo1soB91YywCJHxbDK8Fd4qoKOdr 4pLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=LqiSnF21wkD1mwQUEqS6H3zY3dKSMjfAJKqDZ7NDwdI=; b=WjRn3KB2TyjY2cP7JrN2mkPLT3d1dhPMV5VEKlm7kn8SYgclXSofwJPZAWHDP8g9OW f5PRR3qxzzt3cVA0R+OwXLkBvt7B4Kl3FERvWzIIVHJ+bnvj9n5Z3hZck7J723aIR/9h IWUs1mHd7ndLl2Uto628N6JZvpGF/Jt21CoIhgyAxhO6J5iztAsuU8g+R3GAv2/uzgD9 GaLTOw7N/W8zbd5g1wFgvtdECTGDZus5iMCoESmK3LrK8OnNrDYhp7oqs1HrcnqILeHz Eq2dTa7kdaEtdu/vMAx/8wfZo9VqteHNP1q6ZNsLnlZwqpRswTUwedWWc/omQltkaV/h ar4g== X-Gm-Message-State: ALyK8tJLpMRuaCfRw9QWHKGDS8G4AZ3cu6SMFWXKmsdVghEEFyaLsArg6bQWtmxr1lOqeiW5jNJtcmLS2H5G5Q== X-Received: by 10.202.206.12 with SMTP id e12mr1057855oig.160.1466732882249; Thu, 23 Jun 2016 18:48:02 -0700 (PDT) MIME-Version: 1.0 Received: by 10.157.9.8 with HTTP; Thu, 23 Jun 2016 18:48:01 -0700 (PDT) In-Reply-To: References: <555A9752-4A3E-44A3-9E6A-45597DA2CB1E@gmail.com> From: Gary Gregory Date: Thu, 23 Jun 2016 18:48:01 -0700 Message-ID: Subject: Re: Lock pattern To: Log4J Developers List Content-Type: multipart/alternative; boundary=001a113d24266d60ee0535fc5c1e archived-at: Fri, 24 Jun 2016 01:48:10 -0000 --001a113d24266d60ee0535fc5c1e Content-Type: text/plain; charset=UTF-8 On Thu, Jun 23, 2016 at 5:52 PM, Matt Sicker wrote: > I kind of hate to point this out, but what's wrong with this when you're > not using multiple condition variables? > > final Object lock = new Object(); > synchronized (lock) { > // ... > } > Considering that we use a lot of ReentrantLocks, does this apply? We cannot replace ReentrantLocks with Objects. Or is that what you are proposing? I'm guessing you are making a more general point that a AutoCloseableLock is not a generic replacement for synchronized blocks. Gary > > It auto-unlocks at the end, too. > > On 23 June 2016 at 19:35, Gary Gregory wrote: > >> On Thu, Jun 23, 2016 at 4:58 PM, Gary Gregory >> wrote: >> >>> On Thu, Jun 23, 2016 at 4:56 PM, Gary Gregory >>> 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. >>> >> >> But let's say you do for GC-free reasons: >> >> public AutoCloseableLock lock() { >> this.lock.lock(); >> return this; >> } >> >> to do: >> >> private AutoCloseableLock autoCloseableLock = new >> AutoCloseableLock(new ReentrantLock()); >> >> public static boolean hasManager(final String name) { >> try (Object o = autoCloseableLock.lock()) { >> return MAP.containsKey(name); >> } >> } >> >> Gary >> >> 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 wrote: >>>> >>>>> Sounds like an opportunity for a small Commons or independent project. >>>>> CloseableLock! >>>>> >>>>> On 23 June 2016 at 18:51, Greg Thomas 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 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 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: garydgregory@gmail.com | ggregory@apache.org >>>>>> Java Persistence with Hibernate, Second Edition >>>>>> >>>>>> JUnit in Action, Second Edition >>>>>> Spring Batch in Action >>>>>> Blog: http://garygregory.wordpress.com >>>>>> Home: http://garygregory.com/ >>>>>> Tweet! http://twitter.com/GaryGregory >>>>>> >>>>>> >>>>> >>>>> >>>>> -- >>>>> Matt Sicker >>>>> >>>> >>>> >>>> >>>> -- >>>> E-Mail: garydgregory@gmail.com | ggregory@apache.org >>>> Java Persistence with Hibernate, Second Edition >>>> >>>> JUnit in Action, Second Edition >>>> Spring Batch in Action >>>> Blog: http://garygregory.wordpress.com >>>> Home: http://garygregory.com/ >>>> Tweet! http://twitter.com/GaryGregory >>>> >>> >>> >>> >>> -- >>> E-Mail: garydgregory@gmail.com | ggregory@apache.org >>> Java Persistence with Hibernate, Second Edition >>> >>> JUnit in Action, Second Edition >>> Spring Batch in Action >>> Blog: http://garygregory.wordpress.com >>> Home: http://garygregory.com/ >>> Tweet! http://twitter.com/GaryGregory >>> >> >> >> >> -- >> E-Mail: garydgregory@gmail.com | ggregory@apache.org >> Java Persistence with Hibernate, Second Edition >> >> JUnit in Action, Second Edition >> Spring Batch in Action >> Blog: http://garygregory.wordpress.com >> Home: http://garygregory.com/ >> Tweet! http://twitter.com/GaryGregory >> > > > > -- > Matt Sicker > -- E-Mail: garydgregory@gmail.com | ggregory@apache.org Java Persistence with Hibernate, Second Edition JUnit in Action, Second Edition Spring Batch in Action Blog: http://garygregory.wordpress.com Home: http://garygregory.com/ Tweet! http://twitter.com/GaryGregory --001a113d24266d60ee0535fc5c1e Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On T= hu, Jun 23, 2016 at 5:52 PM, Matt Sicker <boards@gmail.com> w= rote:
I kind of hate to point this out= , but what's wrong with this when you're not using multiple conditi= on variables?

final Object lock =3D new Object();
<= div>synchronized (lock) {
=C2=A0 // ...
}

Considering that we use a lot of ReentrantLo= cks, does this apply? We cannot replace ReentrantLocks with Objects. Or is = that what you are proposing? I'm guessing you are making a more general= point that a=C2=A0AutoCloseableLock is not a generic replacement for synch= ronized blocks.

Gary

It auto-unlocks at the end, too.
<= div class=3D"gmail_extra">

On 23 June 2016 at 19:35, Gary Gregory <garydgregory@gmail.com= > wrote:
On Thu, Jun 23, 2016 at 4:58 P= M, Gary Gregory <garydgregory@gmail.com> wrote:
On Thu, Jun 23, 2016 at 4:56 PM, Gary Gregory <garydgr= egory@gmail.com> wrote:
= Like this:

=C2=A0 = =C2=A0 public static boolean hasManager(final String name) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 try (Object o =3D= =C2=A0AutoCloseableLock.lock(LOCK)) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return MAP.containsKey(name);=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 }

With= a new class:

public class AutoCloseableLock = implements AutoCloseable {
=C2=A0 =C2=A0=C2=A0
=C2=A0 = =C2=A0 public static AutoCloseableLock lock(final Lock lock) {
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 Objects.requireNonNull(lock, "lock");=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 lock.lock();
=C2=A0 =C2=A0= =C2=A0 =C2=A0 return new AutoCloseableLock(lock);
=C2=A0 =C2=A0 = }

=C2=A0 =C2=A0 private final Lock lock;

=C2=A0 =C2=A0 public AutoCloseableLock(final Lock lock) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 this.lock =3D lock;
=C2=A0 = =C2=A0 }

=C2=A0 =C2=A0 @Override
=C2=A0 = =C2=A0 public void close() {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 this.loc= k.unlock();
=C2=A0 =C2=A0 }

=C2=A0 =C2= =A0 public void lock() {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 this.lock.lo= ck();
=C2=A0 =C2=A0 }
You do not even need a lock() method.
=

But let's say you do for = GC-free reasons:

=C2=A0 =C2=A0 public AutoCloseabl= eLock lock() {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 this.lock.lock();
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return this;
=C2=A0 =C2=A0 }

to do:

=C2=A0 =C2=A0 privat= e AutoCloseableLock autoCloseableLock =3D new AutoCloseableLock(new Reentra= ntLock());

=C2=A0= =C2=A0 public static boolean hasManager(final String name) {
<= div style=3D"font-size:12.8px">=C2=A0 =C2=A0 =C2=A0 =C2=A0 try (Object o = =3D=C2=A0autoCloseableLock.lock()) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return MAP.containsKey(name);<= /div>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 }

=C2=A0Gary

<= /div>
Gary=C2=A0
}
<= /div>

The pro is less code and less chance for err= ors, the con is that you create a new AutoCloseableLock for lock/unlock sit= es.

Gary

On Thu, Jun 23, 2016 at 4:54 PM, Matt Sicker <<= a href=3D"mailto:boards@gmail.com" target=3D"_blank">boards@gmail.com&g= t; wrote:
Sounds like an opport= unity for a small Commons or independent project. CloseableLock!

On 23 June 2= 016 at 18:51, Greg Thomas <greg.d.thomas@gmail.com> wr= ote:
I''m sure I read so= mewhere that it was a deliberate choice not to make it, to stop people usin= g the very common pattern of creating the object in the try() - which isn&#= 39;t much use for a lock.=C2=A0

Greg--=C2=A0
Sent from my iPhone
=
On 24 Jun 2016, at 00:45, Remko Popma <remko.popma@gmail.com> wrote:

<= /div>
Good idea!=C2=A0
Maybe p= ropose this for Java 9?
Looks very reasonable to me.=C2=A0
Sent from my iPhone

On 2016/06/24, at 8:32, Gary Gregory <= ;garydgregory@g= mail.com> wrote:

I wonder if anyone knows why Lock is not AutoCloseable.

<= /div>
This:

=C2=A0 =C2=A0 public static b= oolean hasManager(final String name) {
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 LOCK.lock();
=C2=A0 =C2=A0 =C2=A0 =C2=A0 try {
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return MAP.containsKey(name);
<= div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 } finally {
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 LOCK.unlock();
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }=
=C2=A0 =C2=A0 }


Seems la= me in comparison to:

=C2=A0 =C2=A0 public sta= tic boolean hasManager(final String name) {
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 try (LOCK.lock()) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 return MAP.containsKey(name);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 }

Which, due to synta= x really would be:

=C2=A0 =C2=A0 public = static boolean hasManager(final String name) {
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 try (Object o =3D LOCK.lock()) {
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 return MAP.containsKey(name);
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 }

<= div>Just wonderin'...

Gary
=


--
Matt Sicker <boards@gmail.com>



--



--
Matt Sicker <boards@gmail.com>



--
--001a113d24266d60ee0535fc5c1e--