Thanks for your explanation.

On Wed, Jul 10, 2013 at 11:51 PM, Jordan Zimmerman <
[email protected]> 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 <[email protected]> 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

Reply via email to