移动端并发编程基础篇-锁介绍

阻塞算法:

     在并发上下文中,在算法中如果一个线程的挂起导致其它的线程挂起,我们就说这个算法是阻塞的。

非阻塞算法:

    在并发上下文中,在算法中如果一个线程的挂起没有导致其它的线程挂起,我们就说这个算法是非阻塞的。

阻塞算法的效率要高于非阻塞算法。

java多线程编程是基于非阻塞算法的实现。


1.读写锁 WriteLock,ReadLock基本概念

   读锁 -> 共享锁 相同一把读锁,锁住下面逻辑后,如果其他线程还想进入下面的逻辑,不用阻塞等待,可以直接进入到逻辑访问

   写锁 -> 排它锁  相同一把写锁,锁住下面的逻辑后,如果其他线程想进入到下面的逻辑,需要阻塞等待前面的释放锁逻辑

   公平锁 ->  阻塞住后,会有阻塞的顺序编号,如果锁释放后,按照顺序进行

   非公平锁 -> 阻塞后,没有阻塞顺序编号,如果锁释放后,线程开始争夺资源无序访问

2.读锁实例 

volatile private int data = 100;

ReentrantReadWriteLock lock = new ReentrantReadWriteLock();   -> 公平锁

 private void readLockData(){
for(int i = 0;i < 10;i++){
Thread thread = new Thread("thread-" + i){
@Override
public void run() {
super.run();
lock.readLock().lock();//读锁,加锁
int random = (int) (Math.random() * 100);  
ReadLockDemo.this.data = random;
System.out.println("write data -> \t" + random + "\t tname:" + getName());
try {
Thread.sleep(1*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("readData -> \t" + ReadLockDemo.this.data + "\t tName:" + getName());
lock.readLock().unlock();//读锁,释放锁
}
};
thread.start();
}
}

多个线程在读锁加锁的情况下开始执行赋值写操作,没有阻塞状态。1s后,开始执行读操作,读取的内容为最后修改的内容,最后内容是19,所以所有线程读取的内容全部是19

输出结果:

write data -> 69tname:thread-1
write data -> 26tname:thread-2
write data -> 1tname:thread-4
write data -> 14tname:thread-3
write data -> 52tname:thread-0
write data -> 23tname:thread-5
write data -> 55tname:thread-7
write data -> 70tname:thread-8
write data -> 89tname:thread-6
write data -> 19tname:thread-9
readData -> 19tName:thread-8
readData -> 19tName:thread-6
readData -> 19tName:thread-4
readData -> 19tName:thread-3
readData -> 19tName:thread-2
readData -> 19tName:thread-0
readData -> 19tName:thread-1
readData -> 19tName:thread-9
readData -> 19tName:thread-7
readData -> 19tName:thread-5


3.写锁实例

volatile private int data = 100;

ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

for(int i = 0;i < 10;i++){
Thread thread = new Thread("thread-" + i){
@Override
public void run() {
super.run();
lock.writeLock().lock();//写锁,加锁
int random = (int) (Math.random() * 100);
ReadLockDemo.this.data = random;
System.out.println("write data -> \t" + random + "\t tname:" + getName());
try {
Thread.sleep(1*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("readData -> \t" + ReadLockDemo.this.data + "\t tName:" + getName());
lock.writeLock().unlock();//写锁,释放锁
}
};
thread.start();
}

写锁加锁后,多个线程抢占一个写锁,如果上面的一个线程锁住,其他线程线程需要等待这把写锁的释放,才能进入到下面的逻辑,否则就是无限的阻塞状态。

运行输入内容:

write data -> 76tname:thread-0
readData -> 76tName:thread-0
write data -> 84tname:thread-1
readData -> 84tName:thread-1
write data -> 79tname:thread-3
readData -> 79tName:thread-3
write data -> 67tname:thread-4
readData -> 67tName:thread-4
write data -> 89tname:thread-2
readData -> 89tName:thread-2
write data -> 11tname:thread-5
readData -> 11tName:thread-5
write data -> 72tname:thread-7
readData -> 72tName:thread-7
write data -> 40tname:thread-6
readData -> 40tName:thread-6
write data -> 66tname:thread-8
readData -> 66tName:thread-8
write data -> 4tname:thread-9
readData -> 4 tName:thread-9


4.ReentrantLock和ReentWriteReadLock区别

  ReentrantLock属于排它锁,可以进行初始化为公平锁或者非公平锁,new ReentrantLock(boolean fair)进行初始化获取

  ReentWriteReadLock 属于排它锁同时也包含共享锁操作,内部机制是公平锁


  private ReentrantLock lock = new ReentrantLock(true);
  volatile private int data = 0;

  for(int i = 0;i < 20;i++){
Thread thread = new Thread("thread-" + i){
@Override
public void run() {
super.run();
System.out.println("who thread waiting -> " + getName());
lock.lock();//sync.acquire(1);
//write data
int r = (int) (Math.random() * 100);
ReentLockDemo.this.data = r;
System.out.println("write data ->\t" + r + " threadName->" + getName());

try {
Thread.sleep(1*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println("read data ->\t" + ReentLockDemo.this.data + " threadName->" + getName());
lock.unlock();
}
};
thread.start();
}

公平锁,执行加锁顺序按照线程的waiting顺序执行...

 输入结果:

who thread waiting -> thread-0
who thread waiting -> thread-2
who thread waiting -> thread-5
who thread waiting -> thread-1
who thread waiting -> thread-9
who thread waiting -> thread-13
who thread waiting -> thread-6
who thread waiting -> thread-17
who thread waiting -> thread-7
write data -> 18 threadName->thread-0
who thread waiting -> thread-3
who thread waiting -> thread-10
who thread waiting -> thread-4
who thread waiting -> thread-11
who thread waiting -> thread-14
who thread waiting -> thread-16
who thread waiting -> thread-8
who thread waiting -> thread-15
who thread waiting -> thread-18
who thread waiting -> thread-12
who thread waiting -> thread-19
read data -> 18 threadName->thread-0
write data -> 47 threadName->thread-2
read data -> 47 threadName->thread-2
write data -> 25 threadName->thread-1
read data -> 25 threadName->thread-1
write data -> 64 threadName->thread-5
read data -> 64 threadName->thread-5
write data -> 57 threadName->thread-9
read data -> 57 threadName->thread-9
write data -> 54 threadName->thread-13
read data -> 54 threadName->thread-13
write data -> 11 threadName->thread-6
read data -> 11 threadName->thread-6
write data -> 21 threadName->thread-17
read data -> 21 threadName->thread-17
write data -> 27 threadName->thread-7
read data -> 27 threadName->thread-7
write data -> 43 threadName->thread-3
read data -> 43 threadName->thread-3
write data -> 30 threadName->thread-10
read data -> 30 threadName->thread-10
write data -> 46 threadName->thread-4
read data -> 46 threadName->thread-4
write data -> 16 threadName->thread-11
read data -> 16 threadName->thread-11
write data -> 27 threadName->thread-14
read data -> 27 threadName->thread-14
write data -> 65 threadName->thread-16
read data -> 65 threadName->thread-16
write data -> 15 threadName->thread-8
read data -> 15 threadName->thread-8
write data -> 14 threadName->thread-15
read data -> 14 threadName->thread-15
write data -> 69 threadName->thread-18
read data -> 69 threadName->thread-18
write data -> 88 threadName->thread-12
read data -> 88 threadName->thread-12
write data -> 62 threadName->thread-19
read data -> 62 threadName->thread-19



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值