Java锁之可重入锁介绍
2016-03-30 16:30
375 查看
锁作为并发共享数据,保证一致性的工具,在JAVA平台有多种实现(如 synchronized 和 ReentrantLock等等 ) 。这些已经写好提供的锁为我们开发提供了便利,但是锁的具体性质以及类型却很少被提及。本系列文章将分析JAVA下常见的锁名称以及特性,为大家答疑解惑。
四、可重入锁:
本文里面讲的是广义上的可重入锁,而不是单指JAVA下的ReentrantLock。
可重入锁,也叫做递归锁,指的是同一线程 外层函数获得锁之后 ,内层递归函数仍然有获取该锁的代码,但不受影响。
在JAVA环境下 ReentrantLock 和synchronized 都是 可重入锁。
下面是使用实例:
复制代码 代码如下:
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();
}
}
两个例子最后的结果都是正确的,即 同一个线程id被连续输出两次。
结果如下:
复制代码 代码如下:
Threadid: 8
Threadid: 8
Threadid: 10
Threadid: 10
Threadid: 9
Threadid: 9
可重入锁最大的作用是避免死锁。
我们以自旋锁作为例子。
复制代码 代码如下:
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);
}
}
}
}
该自旋锁即为可重入锁。
===========================
四、可重入锁:
本文里面讲的是广义上的可重入锁,而不是单指JAVA下的ReentrantLock。
可重入锁,也叫做递归锁,指的是同一线程 外层函数获得锁之后 ,内层递归函数仍然有获取该锁的代码,但不受影响。
在JAVA环境下 ReentrantLock 和synchronized 都是 可重入锁。
下面是使用实例:
复制代码 代码如下:
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();
}
}
两个例子最后的结果都是正确的,即 同一个线程id被连续输出两次。
结果如下:
复制代码 代码如下:
Threadid: 8
Threadid: 8
Threadid: 10
Threadid: 10
Threadid: 9
Threadid: 9
可重入锁最大的作用是避免死锁。
我们以自旋锁作为例子。
复制代码 代码如下:
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);
}
}
}
}
该自旋锁即为可重入锁。
===========================
public class MyLock { private AtomicReference owner = new AtomicReference(); private int count = 0; public void lock(){ Thread thread = Thread.currentThread(); if(thread == owner.get()){ count++; return; } while(!owner.compareAndSet(null,thread)){ } } public void unlock() { Thread thread = Thread.currentThread(); /*if(thread == owner.get()){ count--; if(count == 0){ owner.compareAndSet(thread,null); } }*/ if (thread == owner.get()) { if (count != 0) { count--; } else { owner.compareAndSet(thread, null); } } } }
public class Test { static MyLock mylock = new MyLock(); public static void main(String[] args){ try { TestLock t = new TestLock(); t.start(); TestLock1 t1 = new TestLock1(); t1.start(); }catch(Exception e){ } } public static class TestLock extends Thread{ public void run(){ for(int i = 0 ; i< 55 ; i++){ mylock.lock(); System.out.println("t锁定"); try { Thread.sleep(500); }catch (Exception e){ } System.out.println("t解锁"); mylock.unlock(); } } } public static class TestLock1 extends Thread{ public void run() { for (int i = 0; i < 55; i++) { mylock.lock(); System.out.println("t1锁定"); try { Thread.sleep(500); } catch (Exception e) { } System.out.println("t1解锁"); mylock.unlock(); } } } }
相关文章推荐
- javacript属性
- JAVA中this用法思考
- java 中的写入后换行
- Struts2 实现Json返回
- 解决spring-mvc @responseBody注解返回json 乱码问题
- 语言基础——Java_binarySearch(折半查找)
- i++与++i的区别
- 关于C与Java的输出代码
- java中的缓存技术该如何实现
- 使用java 和freemarker实现多级菜单(分类)
- 阶乘之和
- 详解Java Spring各种依赖注入注解的区别
- springmvc+mybatis学习笔记(汇总)
- int与Integer类型关系
- javaweb学习总结(二十三)——jsp自定义标签开发入门
- 字符串数组元素提取比较
- Java中Runnable和Thread的区别
- 指定数组中的数据比大小
- Spring MVC + MyBatis整合(IntelliJ IDEA环境下)
- Java transient关键字使用小记