curator-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chao chu <chuchao...@gmail.com>
Subject The Helper trick in HandleHolder
Date Wed, 10 Jul 2013 14:40:52 GMT
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

Mime
View raw message