Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有人能用一些最好的例子解释一下如何在java中使用重入锁吗_Java_Multithreading_Concurrency_Reentrantlock - Fatal编程技术网

有人能用一些最好的例子解释一下如何在java中使用重入锁吗

有人能用一些最好的例子解释一下如何在java中使用重入锁吗,java,multithreading,concurrency,reentrantlock,Java,Multithreading,Concurrency,Reentrantlock,当我在运行示例类时,我看到的行为与synchronized相同。不,您通常不会看到行为上的差异。但正如该网站所说,在一些情况下,您需要使用ReentrantLock而不是synchronized 您希望公平地选择等待线程 您想使用tryLock()方法 您想中断正在等待的线程并让它执行其他操作 ReentrantLock的性能比synchronized好,这一点您很关心 如果您不需要任何这些改进,那么使用synchronized就可以了,而且您可能无法区分它们之间的区别。来自: 具有相同基本行为

当我在运行示例类时,我看到的行为与
synchronized

相同。不,您通常不会看到行为上的差异。但正如该网站所说,在一些情况下,您需要使用
ReentrantLock
而不是
synchronized

  • 您希望公平地选择等待线程
  • 您想使用tryLock()方法
  • 您想中断正在等待的线程并让它执行其他操作
  • ReentrantLock的性能比synchronized好,这一点您很关心
  • 如果您不需要任何这些改进,那么使用
    synchronized
    就可以了,而且您可能无法区分它们之间的区别。

    来自:

    具有相同基本行为和特征的可重入互斥 使用
    synchronized
    方法和语句,但具有扩展功能


    在您的示例中,您不使用“扩展功能”;您使用
    ReentrantLock
    作为
    synchronized
    方法的等效替代方法(除了使用
    synchronized
    语句时,您使用
    this
    作为锁)。因此这两种方法的行为必须相同。

    这里有三种方法,一种是线程访问锁的方法,另一种是释放锁的方法。您可能希望尝试使用
    synchronized
    关键字实现这些功能。使用
    ReentrantLock
    的扩展功能和优势将变得显而易见

    public class DoorLockUsingLock {
    
        private int counter= 0;
        private Thread owner= null;
        private Lock l = new ReentrantLock();
        private Condition notLocked= l.newCondition();
    
        public void lockItDown() throws InterruptedException {
            l.lockInterruptibly();
            try {
                while ((counter> 0) && (owner!= Thread.currentThread())) {
                    notLocked.await();
                }
                counter++;
                owner = Thread.currentThread();
            } finally {
                l.unlock();
            }
        }
    
        public void lockItDownUninterruptibly() {
            l.lock();
            try {
                while ((counter > 0) && (owner != Thread.currentThread())) {
                    notLocked.awaitUninterruptibly();
                }
                counter++;
                owner= Thread.currentThread();
            } finally {
                l.unlock();
            }
        }
    
        public boolean tryLockItDown(long timeout, TimeUnit unit) throws InterruptedException {
            long time = unit.toNanos(timeout);
            long end = System.nanoTime() + time;
            boolean success = l.tryLock(timeout, unit);
            if (!success) {
                return false;
            }
            try {
                time = end- System.nanoTime();
                while ((counter> 0) && (owner != Thread.currentThread()) && (time > 0)) {
                    notLocked.await(time, TimeUnit.NANOSECONDS);
                    time = end - System.nanoTime();
                }
                if (time > 0) {
                    counter++;
                    owner = Thread.currentThread();
                    return true;
                }
                return false;
            } finally {
                l.unlock();
            }
        }
    
        public void unlockIt() throws IllegalMonitorStateException {
            l.lock();
            try {
                if (counter== 0) {
                    throw new IllegalMonitorStateException();
                }
                if (owner!= Thread.currentThread()) {
                    throw new IllegalMonitorStateException();
                }
                counter--;
                if (counter == 0) {
                    owner = null;
                    notLocked.signal();
                }
            } finally {
                l.unlock();
            }
        }
    }
    

    这基本上再现了
    同步
    等待
    通知
    行为
    ReentrantLock
    Condition
    清除了语义并允许公平性,但在其他方面基本相同。@Davidermann公平排队只是故事的一部分
    ReentrantLock
    还允许无条件、轮询、定时或可中断的锁获取。在重启JVM之前,不可能退出死锁的内部锁。这是非常有用的,
    ReentrantReadWriteLock
    。使用
    synchronized
    实现这一点很烦人。