gpt4 book ai didi

java - java中的可重入锁

转载 作者:行者123 更新时间:2023-12-02 04:21:57 25 4
gpt4 key购买 nike

我是java中多线程的新手。我试图使用锁。这是我的代码示例。

package com;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class UsingLocks {
Lock lock = new ReentrantLock();
public static void main(String[] args) {
// TODO Auto-generated method stub


UsingLocks job = new UsingLocks();
Thread [] threads= new Thread[5];
for(int i=0;i<5;i++){
threads[i]= new Thread(new LockTask(job));
}
for(int i=0;i<5;i++){
threads[i].start();
}

}

public void lockingJob() {
System.out.println("Thread "+Thread.currentThread().getName()+" trying to Acquire lock");
try {
lock.tryLock();
//lock.lock(); //When I use this, code works fine
int time=new Random().nextInt(10)+3;
System.out.println("Thread "+Thread.currentThread().getName()+" Acquired lock for "+time+" seconds.");
TimeUnit.SECONDS.sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Now releasing lock "+Thread.currentThread().getName());
lock.unlock();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("After Unlock "+Thread.currentThread().getName());



}




}
class LockTask implements Runnable{
UsingLocks job;
public LockTask(UsingLocks job) {
// TODO Auto-generated constructor stub
this.job=job;

}

@Override
public void run() {
// TODO Auto-generated method stub
job.lockingJob();


}

}

以下是我使用 tryLock() 时的输出

Thread Thread-1 trying to Acquire lock
Thread Thread-0 trying to Acquire lock
Thread Thread-2 trying to Acquire lock
Thread Thread-1 Acquired lock for 12 seconds.
Thread Thread-2 Acquired lock for 3 seconds.
Thread Thread-0 Acquired lock for 8 seconds.
Thread Thread-3 trying to Acquire lock
Thread Thread-3 Acquired lock for 9 seconds.
Thread Thread-4 trying to Acquire lock
Thread Thread-4 Acquired lock for 6 seconds.
Now releasing lock Thread-2
Exception in thread "Thread-2" java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:155)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1260)
at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:460)
at com.UsingLocks.lockingJob(UsingLocks.java:37)
at com.LockTask.run(UsingLocks.java:66)
at java.lang.Thread.run(Thread.java:745)
Now releasing lock Thread-4
Exception in thread "Thread-4" java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:155)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1260)
at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:460)
at com.UsingLocks.lockingJob(UsingLocks.java:37)
at com.LockTask.run(UsingLocks.java:66)
at java.lang.Thread.run(Thread.java:745)

现在,根据我的理解,当第一个线程执行 tryLock() 时,它应该获取锁,其他线程不应该获取锁。但是如输出所示。Thread-1 获取锁后,Thread -2还获得了锁等等。这怎么可能。请告诉我我在这里缺少什么。提前致谢。

最佳答案

原因是如果锁已被另一个线程持有,tryLock 永远不会阻塞。

下面是有关 tryLock() 的文档

public boolean tryLock()

仅当调用时锁未被其他线程持有时才获取锁。

如果锁未被其他线程持有,则获取该锁并立即返回 true 值,并将锁持有计数设置为 1。即使此锁已设置为使用公平排序策略,对 tryLock() 的调用也将立即获取该锁(如果该锁可用),无论其他线程当前是否正在等待该锁。这种“闯入”行为在某些情况下可能有用,尽管它破坏了公平性。如果您想遵守此锁的公平性设置,请使用几乎等效的 tryLock(0, TimeUnit.SECONDS)(它还检测中断)。

如果当前线程已持有此锁,则持有计数将增加 1,并且该方法返回 true。

如果锁被另一个线程持有,则此方法将立即返回 false 值。

关于java - java中的可重入锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32680954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com