curator-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chao chu <chuchao...@gmail.com>
Subject Re: The Helper trick in HandleHolder
Date Wed, 10 Jul 2013 17:21:39 GMT
Thanks for your explanation.


On Wed, Jul 10, 2013 at 11:51 PM, Jordan Zimmerman <
jordan@jordanzimmerman.com> wrote:

> I don't know how common it is, but it's something I use a lot ;) Yes, it
> avoids sync.
>
> -JZ
>
> On Jul 10, 2013, at 7:40 AM, chao chu <chuchao333@gmail.com> wrote:
>
> Hi Jordan,
>
> Please forgive my ignorance, in the below code, it's just used to avoid
> unnecessary synchronizations?
>
> I didn't see similar code before, is this a common pattern/idiom in Java?
> Thanks
>
>
>
>         // first helper is synchronized when getZooKeeper is called. Subsequent calls
>         // are not synchronized.
>
>
>         helper = new Helper()
>
>         {
>             private volatile ZooKeeper zooKeeperHandle = null;
>
>
>             private volatile String connectionString = null;
>
>
>
>             @Override
>             public ZooKeeper getZooKeeper() throws Exception
>
>
>             {
>                 synchronized(this)
>
>
>                 {
>                     if ( zooKeeperHandle == null )
>
>
>                     {
>                         connectionString = ensembleProvider.getConnectionString();
>
>
>                         zooKeeperHandle = zookeeperFactory.newZooKeeper(connectionString,
sessionTimeout, watcher, canBeReadOnly);
>
>
>                     }
>
>
>
>                     helper = new Helper()
>
>                     {
>                         @Override
>
>                         public ZooKeeper getZooKeeper() throws Exception
>
>
>                         {
>                             return zooKeeperHandle;
>
>
>                         }
>
>
>
>                         @Override
>                         public String getConnectionString()
>
>
>                         {
>                             return connectionString;
>
>
>                         }
>                     };
>
>
>                     return zooKeeperHandle;
>
>                 }
>             }
>
>
>             @Override
>             public String getConnectionString()
>
>
>             {
>                 return connectionString;
>
>
>             }
>         };
>
>
> --
> ChuChao
>
>
>


-- 
ChuChao

Mime
View raw message