⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reentrantlock.java

📁 jboss规则引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at * http://creativecommons.org/licenses/publicdomain */package org.drools.util.concurrent.locks;import java.io.Serializable;import java.util.Collection;/** *  This is a stripped down version of jdk1.5 ReentrantLock.  *  All the condition and wait stuff has been removed * * @since 1.5 * @author Doug Lea * @author Dawid Kurzyniec */public class ReentrantLock    implements    Lock,    java.io.Serializable {    private static final long serialVersionUID = 7373984872572414699L;    private final NonfairSync sync;    final static class NonfairSync implements Serializable {        private static final long  serialVersionUID = 7316153563782823691L;        protected transient Thread owner_           = null;        protected transient int    holds_           = 0;        final void incHolds() {            final int nextHolds = ++this.holds_;            if ( nextHolds < 0 ) {                throw new Error( "Maximum lock count exceeded" );            }            this.holds_ = nextHolds;        }        public boolean tryLock() {            final Thread caller = Thread.currentThread();            synchronized ( this ) {                if ( this.owner_ == null ) {                    this.owner_ = caller;                    this.holds_ = 1;                    return true;                } else if ( caller == this.owner_ ) {                    incHolds();                    return true;                }            }            return false;        }        public synchronized int getHoldCount() {            return isHeldByCurrentThread() ? this.holds_ : 0;        }        public synchronized boolean isHeldByCurrentThread() {            return this.holds_ > 0 && Thread.currentThread() == this.owner_;        }        public synchronized boolean isLocked() {            return this.owner_ != null;        }        protected synchronized Thread getOwner() {            return this.owner_;        }        public boolean hasQueuedThreads() {            throw new UnsupportedOperationException( "Use FAIR version" );        }        public int getQueueLength() {            throw new UnsupportedOperationException( "Use FAIR version" );        }        public Collection getQueuedThreads() {            throw new UnsupportedOperationException( "Use FAIR version" );        }        public boolean isQueued(final Thread thread) {            throw new UnsupportedOperationException( "Use FAIR version" );        }        public void lock() {            final Thread caller = Thread.currentThread();            synchronized ( this ) {                if ( this.owner_ == null ) {                    this.owner_ = caller;                    this.holds_ = 1;                    return;                } else if ( caller == this.owner_ ) {                    incHolds();                    return;                } else {                    boolean wasInterrupted = Thread.interrupted();                    try {                        while ( true ) {                            try {                                wait();                            } catch ( final InterruptedException e ) {                                wasInterrupted = true;                                // no need to notify; if we were signalled, we                                // will act as signalled, ignoring the                                // interruption                            }                            if ( this.owner_ == null ) {                                this.owner_ = caller;                                this.holds_ = 1;                                return;                            }                        }                    } finally {                        if ( wasInterrupted ) {                            Thread.currentThread().interrupt();                        }                    }                }            }        }        public void lockInterruptibly() throws InterruptedException {            if ( Thread.interrupted() ) {                throw new InterruptedException();            }            final Thread caller = Thread.currentThread();            synchronized ( this ) {                if ( this.owner_ == null ) {                    this.owner_ = caller;                    this.holds_ = 1;                    return;                } else if ( caller == this.owner_ ) {                    incHolds();                    return;                } else {                    try {                        do {                            wait();                        } while ( this.owner_ != null );                        this.owner_ = caller;                        this.holds_ = 1;                        return;                    } catch ( final InterruptedException ex ) {                        if ( this.owner_ == null ) {                            notify();                        }                        throw ex;                    }                }            }        }        public synchronized void unlock() {            if ( Thread.currentThread() != this.owner_ ) {                throw new IllegalMonitorStateException( "Not owner" );            }            if ( --this.holds_ == 0 ) {                this.owner_ = null;                notify();            }        }    }    /**     * Creates an instance of <tt>ReentrantLock</tt>.     * This is equivalent to using <tt>ReentrantLock(false)</tt>.     */    public ReentrantLock() {        this.sync = new NonfairSync();    }    /**     * Acquires the lock.     *     * <p>Acquires the lock if it is not held by another thread and returns     * immediately, setting the lock hold count to one.     *     * <p>If the current thread     * already holds the lock then the hold count is incremented by one and     * the method returns immediately.     *     * <p>If the lock is held by another thread then the     * current thread becomes disabled for thread scheduling     * purposes and lies dormant until the lock has been acquired,     * at which time the lock hold count is set to one.     */    public void lock() {        this.sync.lock();    }    /**     * Acquires the lock unless the current thread is     * {@link Thread#interrupt interrupted}.     *     * <p>Acquires the lock if it is not held by another thread and returns     * immediately, setting the lock hold count to one.     *     * <p>If the current thread already holds this lock then the hold count     * is incremented by one and the method returns immediately.     *     * <p>If the lock is held by another thread then the     * current thread becomes disabled for thread scheduling     * purposes and lies dormant until one of two things happens:     *     * <ul>     *     * <li>The lock is acquired by the current thread; or     *     * <li>Some other thread {@link Thread#interrupt interrupts} the current     * thread.     *     * </ul>     *     * <p>If the lock is acquired by the current thread then the lock hold     * count is set to one.     *     * <p>If the current thread:     *     * <ul>     *     * <li>has its interrupted status set on entry to this method; or     *     * <li>is {@link Thread#interrupt interrupted} while acquiring     * the lock,     *     * </ul>     *     * then {@link InterruptedException} is thrown and the current thread's     * interrupted status is cleared.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -