Package edu.emory.mathcs.backport.java.util.concurrent.locks

Examples of edu.emory.mathcs.backport.java.util.concurrent.locks.ReentrantLock


            return removed;
        }

        public int size() {
            int s;
            final ReentrantLock lock = this.lock;
            lock.lock();
            try {
                s = size;
            } finally {
                lock.unlock();
            }
            return s;
        }
View Full Code Here


        public int remainingCapacity() {
            return Integer.MAX_VALUE;
        }

        public Object peek() {
            final ReentrantLock lock = this.lock;
            lock.lock();
            try {
                return queue[0];
            } finally {
                lock.unlock();
            }
        }
View Full Code Here

        public boolean offer(Object x) {
            if (x == null)
                throw new NullPointerException();
            RunnableScheduledFuture e = (RunnableScheduledFuture)x;
            final ReentrantLock lock = this.lock;
            lock.lock();
            try {
                int i = size;
                if (i >= queue.length)
                    grow();
                size = i + 1;
                boolean notify;
                if (i == 0) {
                    notify = true;
                    queue[0] = e;
                    setIndex(e, 0);
                }
                else {
                    notify = e.compareTo(queue[0]) < 0;
                    siftUp(i, e);
                }
                if (notify)
                    available.signalAll();
            } finally {
                lock.unlock();
            }
            return true;
  }
View Full Code Here

        public boolean offer(Object e, long timeout, TimeUnit unit) {
            return offer(e);
        }

        public Object poll() {
            final ReentrantLock lock = this.lock;
            lock.lock();
            try {
                RunnableScheduledFuture first = queue[0];
                if (first == null || first.getDelay(TimeUnit.NANOSECONDS) > 0)
                    return null;
                else
                    return finishPoll(first);
            } finally {
                lock.unlock();
            }
        }
View Full Code Here

                lock.unlock();
            }
        }

        public Object take() throws InterruptedException {
            final ReentrantLock lock = this.lock;
            lock.lockInterruptibly();
            try {
                for (;;) {
                    RunnableScheduledFuture first = queue[0];
                    if (first == null)
                        available.await();
                    else {
                        long delay =  first.getDelay(TimeUnit.NANOSECONDS);
                        if (delay > 0)
                            available.await(delay, TimeUnit.NANOSECONDS);
                        else
                            return finishPoll(first);
                    }
                }
            } finally {
                lock.unlock();
            }
        }
View Full Code Here

        public Object poll(long timeout, TimeUnit unit)
            throws InterruptedException {
            long nanos = unit.toNanos(timeout);
            long deadline = Utils.nanoTime() + nanos;
            final ReentrantLock lock = this.lock;
            lock.lockInterruptibly();
            try {
                for (;;) {
                    RunnableScheduledFuture first = queue[0];
                    if (first == null) {
                        if (nanos <= 0) {
                            return null;
                        } else {
                            available.await(nanos, TimeUnit.NANOSECONDS);
                            nanos = deadline - Utils.nanoTime();
                        }
                    } else {
                        long delay = first.getDelay(TimeUnit.NANOSECONDS);
                        if (delay > 0) {
                            if (nanos <= 0)
                                return null;
                            if (delay > nanos)
                                delay = nanos;
                            available.await(delay, TimeUnit.NANOSECONDS);
                            nanos = deadline - Utils.nanoTime();
                        } else
                            return finishPoll(first);
                    }
                }
            } finally {
                lock.unlock();
            }
        }
View Full Code Here

                lock.unlock();
            }
        }

        public void clear() {
            final ReentrantLock lock = this.lock;
            lock.lock();
            try {
                for (int i = 0; i < size; i++) {
                    RunnableScheduledFuture t = queue[i];
                    if (t != null) {
                        queue[i] = null;
                        setIndex(t, -1);
                    }
                }
                size = 0;
            } finally {
                lock.unlock();
            }
        }
View Full Code Here

        public int drainTo(Collection c) {
            if (c == null)
                throw new NullPointerException();
            if (c == this)
                throw new IllegalArgumentException();
            final ReentrantLock lock = this.lock;
            lock.lock();
            try {
                int n = 0;
                for (;;) {
                    RunnableScheduledFuture first = pollExpired();
                    if (first != null) {
                        c.add(first);
                        ++n;
                    }
                    else
                        break;
                }
                if (n > 0)
                    available.signalAll();
                return n;
            } finally {
                lock.unlock();
            }
        }
View Full Code Here

                throw new NullPointerException();
            if (c == this)
                throw new IllegalArgumentException();
            if (maxElements <= 0)
                return 0;
            final ReentrantLock lock = this.lock;
            lock.lock();
            try {
                int n = 0;
                while (n < maxElements) {
                    RunnableScheduledFuture first = pollExpired();
                    if (first != null) {
                        c.add(first);
                        ++n;
                    }
                    else
                        break;
                }
                if (n > 0)
                    available.signalAll();
                return n;
            } finally {
                lock.unlock();
            }
        }
View Full Code Here

                lock.unlock();
            }
        }

        public Object[] toArray() {
            final ReentrantLock lock = this.lock;
            lock.lock();
            try {
                return Arrays.copyOf(queue, size);
            } finally {
                lock.unlock();
            }
        }
View Full Code Here

TOP

Related Classes of edu.emory.mathcs.backport.java.util.concurrent.locks.ReentrantLock

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.