简单了解Java中的可重入锁

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

简单了解Java中的可重入锁

  2021-04-02 我要评论

这篇文章主要介绍了简单了解Java中的可重入锁,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

本文里面讲的是广义上的可重入锁,而不是单指JAVA下的ReentrantLock。

可重入锁,也叫做递归锁,指的是同一线程 外层函数获得锁之后 ,内层递归函数仍然有获取该锁的代码,但不受影响。

在JAVA环境下 ReentrantLock 和synchronized 都是 可重入锁。

下面是使用实例:

package reentrantLock;
 
public class Test implements Runnable{
 
  public synchronized void get(){
    System.out.println(Thread.currentThread().getId());
    set();
  }
 
  public synchronized void set(){
    System.out.println(Thread.currentThread().getId());
  }
 
  @Override
  public void run() {
    get();
  }
  public static void main(String[] args) {
    Test ss=new Test();
    new Thread(ss).start();
    new Thread(ss).start();
    new Thread(ss).start();
  }
}

运行截图:

package reentrantLock;
 
import java.util.concurrent.locks.ReentrantLock;
 
public class Test implements Runnable {
  ReentrantLock lock = new ReentrantLock();
 
  public void get() {
    lock.lock();
    System.out.println(Thread.currentThread().getId());
    set();
    lock.unlock();
  }
 
  public void set() {
    lock.lock();
    System.out.println(Thread.currentThread().getId());
    lock.unlock();
  }
 
  @Override
  public void run() {
    get();
  }
 
  public static void main(String[] args) {
    Test ss = new Test();
    new Thread(ss).start();
    new Thread(ss).start();
    new Thread(ss).start();
  }
}

可重入锁最大的作用是避免死锁

我们以自旋锁作为例子,

public class SpinLock {
	private AtomicReference<Thread> owner =new AtomicReference<>();
	public void lock(){
		Thread current = Thread.currentThread();
		while(!owner.compareAndSet(null, current)){
		}
	}
	public void unlock (){
		Thread current = Thread.currentThread();
		owner.compareAndSet(current, null);
	}
}

对于自旋锁来说,

1、若有同一线程两调用lock() ,会导致第二次调用lock位置进行自旋,产生了死锁说明这个锁并不是可重入的。(在lock函数内,应验证线程是否为已经获得锁的线程)

2、若1问题已经解决,当unlock()第一次调用时,就已经将锁释放了。实际上不应释放锁。(采用计数次进行统计)修改之后,如下:

public class SpinLock1 {
	private AtomicReference<Thread> owner =new AtomicReference<>();
	private int count =0;
	public void lock(){
		Thread current = Thread.currentThread();
		if(current==owner.get()) {
			count++;
			return ;
		}
 
		while(!owner.compareAndSet(null, current)){
 
		}
	}
	public void unlock (){
		Thread current = Thread.currentThread();
		if(current==owner.get()){
			if(count!=0){
				count--;
			}else{
				owner.compareAndSet(current, null);
			}
 
		}
 
	}
}

该自旋锁即为可重入锁。

您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们