标签:其他 released set 被占用 wait main rgs 许可证 immediate
ReentrantLock与Synchronized区别:
重入锁可以反复进入
lock.lock();
lock.lock();
try{
i++;
}finally{
lock.unlock();
lock.unlock();
}
//RentrantLock.java JDK8
/**
* 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() {
sync.lock();
}
/**
* Attempts to release this lock.
*
* <p>If the current thread is the holder of this lock then the hold
* count is decremented. If the hold count is now zero then the lock
* is released. If the current thread is not the holder of this
* lock then {@link IllegalMonitorStateException} is thrown.
*
* @throws IllegalMonitorStateException if the current thread does not
* hold this lock
*/
public void unlock() {
sync.release(1);
}
如果同一个线程多次获得锁,那么在释放锁的时候,也需要释放相同次数
若释放锁的次数过多,将会得到 java.lang.IllegalMonitorStateException异常
高级功能
中断响应[lockInterruptibly()]:
如果一个线程在等待锁,那么它依然可以收到一个通知,被告知无需再等待,可以停止工作了。
public class IntLock implements Runnable {
public static ReentrantLock lock1 = new ReentrantLock();
public static ReentrantLock lock2 = new ReentrantLock();
int lock;
public IntLock(int lock) {
this.lock = lock;
}
@Override
public void run() {
try {
if (lock == 1) {
lock1.lockInterruptibly();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
lock2.lockInterruptibly();
} else {
lock2.lockInterruptibly();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
lock1.lockInterruptibly();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (lock1.isHeldByCurrentThread()) {
lock1.unlock();
}
if (lock2.isHeldByCurrentThread()) {
lock2.unlock();
}
System.out.println(Thread.currentThread().getId() + ":线程退出");
}
}
public static void main(String[] args) throws InterruptedException {
IntLock r1 = new IntLock(1);
IntLock r2 = new IntLock(2);
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.start();
t2.start();
Thread.sleep(1000);
t2.interrupt();
}
}
锁申请等待限时[tryLock()]:
public class TimeLock implements Runnable{
public static ReentrantLock lock = new ReentrantLock();
@Override
public void run(){
try{
if(lock.tryLock(5, TimeUnit.SECONDS)){
Thread.sleep(6000);
}else{
System.out.println("get lock failed");
}
}catch (InterruptedException e){
e.printStackTrace();
}finally {
if(lock.isHeldByCurrentThread()){
lock.unlock();
}
}
}
public static void main(String[] args) {
TimeLock tl = new TimeLock();
Thread t1 = new Thread(tl);
Thread t2 = new Thread(tl);
t1.start();
t2.start();
}
}
公平锁[public ReentrantLock(boolean fair)]:
方法总结
ReentrantLock重要方法 | 作用 |
---|---|
lock() | 获得锁,若锁以及被占用,则等待 |
lockInterruptibly() | 获得锁,但是优先响应中断 |
tryLock() | 尝试获得锁 |
tryLock(long time, TimeUnit unit) | 在time时间内尝试获得锁 |
unLock() | 释放锁 |
信号量演示例子
public class SempDemo implements Runnable{
final Semaphore semp = new Semaphore(5);
@Override
public void run(){
try{
semp.acquire();
Thread.sleep(2000);
System.out.println(Thread.currentThread().getId()+":done!");
semp.release();
}catch (InterruptedException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
ExecutorService exec = Executors.newFixedThreadPool(20);
final SempDemo demo = new SempDemo();
for(int i=0;i<20;i++){
exec.submit(demo);
}
}
}
关键方法
Semaphore方法 | 方法描述 |
---|---|
acquire() | 从此信号量获取许可证,阻塞直到可用 |
acquireUninterruptibly() | 同acquire(), 但是不响应中断 |
release() | 释放一个许可证 |
标签:其他 released set 被占用 wait main rgs 许可证 immediate
原文地址:https://www.cnblogs.com/YuanJieHe/p/12607836.html