You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@curator.apache.org by chao chu <ch...@gmail.com> on 2013/07/10 16:40:52 UTC
The Helper trick in HandleHolder
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
Re: The Helper trick in HandleHolder
Posted by chao chu <ch...@gmail.com>.
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 <ch...@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
Re: The Helper trick in HandleHolder
Posted by Jordan Zimmerman <jo...@jordanzimmerman.com>.
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 <ch...@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