public void Close()
 {
     if ((int)State.STARTED != Interlocked.CompareExchange(ref state, (int)State.CLOSED, (int)State.STARTED))
     {
         throw new ZKException("Already closed or has not been started");
     }
     _lock.UnLock();
     //从参与者节点列表中移除
     RemoveParticipantNode();
     client.UnSubscribeStateChanges(stateListener);
     cancellationTokenSource.Cancel();
 }
        public void TestDistributedDelayLock()
        {
            _zkClient.CreateRecursive(lockPath, null, CreateMode.Persistent);
            int           index   = 0;
            List <string> msgList = new List <string>();

            for (int i = 0; i < 5; i++)
            {
                Task.Factory.StartNew(() =>
                {
                    ZKClient zkClient1 = ZKClientBuilder.NewZKClient()
                                         .Servers(string.Format("{0}:{1}", TestUtil.ip, TestUtil.port))
                                         .SessionTimeout(10000)
                                         .Build();
                    ZKDistributedDelayLock _lock = ZKDistributedDelayLock.NewInstance(zkClient1, lockPath);
                    autoReset.WaitOne();

                    _lock.Lock();
                    Console.WriteLine(Thread.CurrentThread.Name + ":lock....");
                    msgList.Add(Thread.CurrentThread.Name + ":unlock");
                    try
                    {
                        Thread.Sleep(1000);
                    }
                    catch (ThreadInterruptedException e)
                    {
                    }
                    Console.WriteLine(Thread.CurrentThread.Name + ":unlock....");
                    _lock.UnLock();
                });
                Interlocked.Increment(ref index);
            }

            autoReset.Set();

            int size = TestUtil.WaitUntil(5, () => { return(msgList.Count); }, new TimeSpan(0, 0, 200));

            Assert.True(size == 5);
        }