关于lock锁
2024-09-05 09:51:04
在 jdk1.5 之后,并发包中新增了 Lock 接口(以及相关实现类)用来实现锁功能,Lock 接口提供了与 synchronized 关键字类似的同步功能,但需要在使用时手动获取锁和释放锁。
lock锁 也叫显示锁
大家看下这个lock锁:
lock是个接口
这个接口下面很多锁:
对于lock锁的使用,实现生产者消费者模式的:
package com.toov5.thread; import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; //共享对象
class ResLock {
public boolean flag = true;
public String sex;
public String name;
public Lock lock = new ReentrantLock();
}
class inputThreadLock extends Thread {
public ResLock res; public inputThreadLock(ResLock res) {
this.res = res;
}
@Override
public void run() {
int count = 0;
while (true) {
try {
res.lock.lock();
if (count == 0) {
res.name = "lucy";
res.sex = "girl";
} else {
res.name = "Jack";
res.sex = "boy";
}
count = (count + 1) % 2;
} catch (Exception e) {
}finally{
res.lock.unlock(); //释放锁写在finally里面
} }
}
}
class readThreadLock extends Thread {
public ResLock res; public readThreadLock(ResLock res) {
this.res = res;
}
@Override
public void run() {
while (true) {
try {
res.lock.lock();
System.out.println(res.name + "," + res.sex); } catch (Exception e) {
e.printStackTrace();
}finally {
res.lock.unlock();
} }
}
}
public class LockTest {
public static void main(String[] args) {
Res res = new Res();
inputThread inputThread = new inputThread(res);
readThread readThread = new readThread(res);
inputThread.start();
readThread.start();
}
}
注意 unlock写到finally里面哦
然后lock锁里面的 类似于 notify 和 wait的 Condition
Condition的功能类似于在传统的线程技术中的,Object.wait()和Object.notify()的功能。
使用方法:
package com.toov5.thread; import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; //共享对象
class ResLock {
public boolean flag = true;
public String sex;
public String name;
public Lock lock = new ReentrantLock();
public Condition condition = lock.newCondition();
}
class inputThreadLock extends Thread {
public ResLock res; public inputThreadLock(ResLock res) {
this.res = res;
}
@Override
public void run() {
if (res.flag) {
try {
res.condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int count = 0;
while (true) { try {
res.lock.lock();
if (count == 0) {
res.name = "lucy";
res.sex = "girl";
} else {
res.name = "Jack";
res.sex = "boy";
}
count = (count + 1) % 2;
res.flag=true;
res.condition.signal();
} catch (Exception e) {
}finally{
res.lock.unlock(); //释放锁写在finally里面
} }
}
}
class readThreadLock extends Thread {
public ResLock res; public readThreadLock(ResLock res) {
this.res = res;
}
@Override
public void run() {
while (true) {
try {
res.lock.lock();
if (res.flag) {
res.condition.await();
}
System.out.println(res.name + "," + res.sex);
res.condition.signal();
} catch (Exception e) {
e.printStackTrace();
}finally {
res.lock.unlock();
} }
}
}
public class LockTest {
public static void main(String[] args) {
Res res = new Res();
inputThread inputThread = new inputThread(res);
readThread readThread = new readThread(res);
inputThread.start();
readThread.start();
}
}
执行结果:
Lock与synchronized 关键字的区别
Lock 接口可以尝试非阻塞地获取锁 当前线程尝试获取锁。如果这一时刻锁没有被其他线程获取到,则成功获取并持有锁。
Lock 接口能被中断地获取锁 与 synchronized 不同,获取到锁的线程能够响应中断,当获取到的锁的线程被中断时,中断异常将会被抛出,同时锁会被释放。
Lock 接口在指定的截止时间之前获取锁,如果截止时间到了依旧无法获取锁,则返回。
synchronize重量级锁 不可控制 释放锁 遇到异常 或者代码执行完毕后 释放锁
lock轻量级 可控释放锁 更灵活一些 lock是1.5出来的 比synchronize晚 弥补了一些不足 还有读写锁 重入锁 功能要强大一些
最新文章
- 由一段字符串中查找URL引出——正则表达式
- 【Android UI】Android开发之View的几种布局方式及实践
- 作业三:PSP记录耗时情况
- Java连接Oracle数据库开发银行管理系统【三、实现篇】
- 第5章 绘图基础_5.1-5.4 GDI绘图
- hiberante入门
- orace 取昨天凌晨的日期
- JS数组追加数组采用push.apply的坑
- [ZOJ1610]Count the Colors(线段树,区间染色,单点查询)
- Android编译过程详解(一)
- final(最终、修饰符)
- Intel 的面试经历中国研究院
- 一位90后的自述:如何从年薪3w到30w
- Ubuntu 18 LTS netplan 网络配置
- SAP顾问岗位要求
- PAT 乙级 1010 一元多项式求导 (25) C++版
- HDU6186(线段树)
- Flutter - 创建底部导航栏
- 疯狂JAVA——第三章 数据类型和运算符
- 每月IT摘录201805
热门文章
- jenkins发布java项目
- POJ 3268 Silver Cow Party 最短路
- 【APIO2015】Jakarta Skyscrapers
- Netty学习_Netty框架入门教程:Netty入门之HelloWorld实现
- List遍历时删除遇到的问题
- Window10下Apache2.4的安装和运行
- LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)
- PS 基础知识 什么是Adobe Bridge
- java性能监控工具jconsole-windows
- java中自带时间类使用方法实例 Date,Timestamp,DateFormat