Ejemplo n.º 1
0
        public BlockingBuffer(int capacity)
        {
            _capacity = capacity;

            _buffer   = new Queue <T>(_capacity);
            _lock     = new ReentrantLock();
            _notFull  = _lock.NewCondition();
            _notEmpty = _lock.NewCondition();
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Construct a blocking queue that based on the given regular
 /// <paramref name="queue"/>.
 /// </summary>
 /// <param name="queue">
 /// A regular queue to be wrapped as blocking queue.
 /// </param>
 /// <param name="capacity">
 /// The capacity of the queue. zero (<c>0</c>) to indicate an
 /// unbounded queue.
 /// </param>
 /// <param name="isFair">
 /// <c>true</c> to grant access to longest waiting threads, otherwise
 /// it does not guarantee any particular access order.
 /// </param>
 /// <exception cref="ArgumentNullException">
 /// If <paramref name="queue"/> is <c>null</c>.
 /// </exception>
 /// <exception cref="ArgumentOutOfRangeException">
 /// If <paramref name="capacity"/> is negative.
 /// </exception>
 public BlockingQueueWrapper(IQueue <T> queue, int capacity, bool isFair)
 {
     if (queue == null)
     {
         throw new ArgumentNullException(nameof(queue));
     }
     if (capacity < 0)
     {
         throw new ArgumentOutOfRangeException(
                   nameof(capacity), capacity, "must not be negative.");
     }
     _lock              = new ReentrantLock(isFair);
     _wrapped           = queue;
     _capacity          = capacity;
     _notEmptyCondition = _lock.NewCondition();
     _notFullCondition  = _lock.NewCondition();
 }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            ZkConnection zkConnection        = new ZkConnection("192.168.60.6:2181");
            ZkClient     zkClient            = new ZkClient(ZkConnect, ZkSessionTimeout, ZkConnectionTimeout, new ZkStringSerializer());
            var          dataLock            = new ReentrantLock();
            var          dataExistsOrChanged = dataLock.NewCondition();

            zkClient.SubscribeDataChanges("/testyy", new DataChange(dataLock, dataExistsOrChanged, zkClient));
            //dataExistsOrChanged.Await(TimeSpan.FromMilliseconds(1000));

            Console.Read();
        }
        public static int?WaitUntilLeaderIsElectedOrChanged(
            ZkClient zkClient, string topic, int partition, long timeoutMs, int?oldLeaderOpt = null)
        {
            var leaderLock            = new ReentrantLock();
            var leaderExistsOrChanged = leaderLock.NewCondition();

            if (oldLeaderOpt.HasValue == false)
            {
                Logger.InfoFormat("Waiting for leader to be elected for partition [{0},{1}]", topic, partition);
            }
            else
            {
                Logger.InfoFormat("Waiting for leader for partition [{0},{1}] to be changed from old leader {2}", topic, partition, oldLeaderOpt.Value);
            }

            leaderLock.Lock();
            try
            {
                zkClient.SubscribeDataChanges(ZkUtils.GetTopicPartitionLeaderAndIsrPath(topic, partition), new LeaderExistsOrChangedListener(topic, partition, leaderLock, leaderExistsOrChanged, oldLeaderOpt, zkClient));
                leaderExistsOrChanged.Await(TimeSpan.FromMilliseconds(timeoutMs));

                // check if leader is elected
                var leader = ZkUtils.GetLeaderForPartition(zkClient, topic, partition);
                if (leader != null)
                {
                    if (oldLeaderOpt.HasValue == false)
                    {
                        Logger.InfoFormat("Leader {0} is elected for partition [{1},{2}]", leader, topic, partition);
                    }
                    else
                    {
                        Logger.InfoFormat(
                            "Leader for partition [{0},{1}] is changed from {2} to {3}",
                            topic,
                            partition,
                            oldLeaderOpt.Value,
                            leader);
                    }
                }
                else
                {
                    Logger.ErrorFormat("Timing out after {0} ms since leader is not elected for partition [{1},{2}]", timeoutMs, topic, partition);
                }

                return(leader);
            }
            finally
            {
                leaderLock.Unlock();
            }
        }
Ejemplo n.º 5
0
        protected override void OnServiceConnected()
        {
            Instance = this;
            _enabled = _lock.NewCondition();
            base.OnServiceConnected();
            _lock.Lock();
            _enabled.SignalAll();
            _lock.Unlock();

            var autoGlobal = AutoGlobal.Instance;

            if (autoGlobal != null && autoGlobal.Context != null)
            {
                Toast.MakeText(autoGlobal.Context, "服务启动成功", ToastLength.Long).Show();
            }
        }
Ejemplo n.º 6
0
        protected override void OnServiceConnected()
        {
            Instance = this;
            _enabled = _lock.NewCondition();
            base.OnServiceConnected();
            _lock.Lock();
            _enabled.SignalAll();
            _lock.Unlock();

            var autoGlobal = AutoGlobal.Instance;

            if (autoGlobal?.Context != null)
            {
                Toast.MakeText(autoGlobal.Context, "服务启动成功", ToastLength.Long).Show();
            }

            var i = new Intent();

            i.SetClass(this, typeof(MainActivity));
            StartActivity(i);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// <inheritDoc/>
        /// This method creates an async call to the server, and blocks until the server
        /// has finished annotating the object.
        /// </summary>
        public override void Annotate(Annotation annotation)
        {
            ILock      Lock           = new ReentrantLock();
            ICondition annotationDone = Lock.NewCondition();

            Annotate(Java.Util.Collections.Singleton(annotation), 1, null);
            try
            {
                Lock.Lock();
                annotationDone.Await();
            }
            catch (Exception)
            {
                // Only wait for one callback to complete; only annotating one document
                log.Info("Interrupt while waiting for annotation to return");
            }
            finally
            {
                Lock.Unlock();
            }
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Creates a <tt>PriorityBlockingQueue</tt> with the default
 /// initial capacity (11) that orders its elements according to
 /// their {@linkplain Comparable natural ordering}.
 /// </summary>
 public PriorityBlockingQueue()
 {
     _innerQueue = new PriorityQueue <T>();
     notEmpty    = _lock.NewCondition();
 }