ReentrantLock的使用。

编程入门 行业动态 更新时间:2024-10-28 06:34:48

<a href=https://www.elefans.com/category/jswz/34/1747461.html style=ReentrantLock的使用。"/>

ReentrantLock的使用。

最近在做一个小型的单机项目。需要用到锁。为了增大并发。把ReentrantLock包装了一下。根据资源id。抢对应的锁。本人是菜鸟来的。希望大神指正。感谢。

import org.apache.commons.lang3.StringUtils;import java.util.HashMap;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;public class ReentrantLockUtil {private volatile static HashMap<String, _ReentrantLock> moreReentrantLock = new HashMap<>();// true公平锁private static ReentrantLock lock = new ReentrantLock(true);/*** @param resource 资源id* @param fair     true 公平 false 非公平* @param timeout  获取锁超时时间* @param unit     时间单位*/public static synchronized boolean tryLock(String resource, boolean fair, long timeout, TimeUnit unit) throws InterruptedException {if (StringUtils.isBlank(resource)) {throw new NullPointerException("resource is null");}_ReentrantLock reentrantLock = moreReentrantLock.get(resource);if (Objects.isNull(reentrantLock)) {ReentrantLock newReentrantLock = new ReentrantLock(fair);_ReentrantLock _reentrantLock_ = new _ReentrantLock(newReentrantLock, resource);if (!moreReentrantLock.containsKey(resource)) {moreReentrantLock.put(resource, _reentrantLock_);}// 如果为false 未取得锁  再尝试一次if (!newReentrantLock.tryLock(timeout, unit)) {return newReentrantLock.tryLock(2, unit);}return true;} else {if (!reentrantLock.getReentrantLock().tryLock(timeout, unit)) {return reentrantLock.getReentrantLock().tryLock(2, unit);}return true;}}public static synchronized void unLock(String resource) {if (StringUtils.isBlank(resource)) {return;}_ReentrantLock reentrantLock = moreReentrantLock.get(resource);if (Objects.nonNull(reentrantLock)) {ReentrantLock reentrantLock1 = reentrantLock.getReentrantLock();// 当前线程才能unlockif (reentrantLock1.isHeldByCurrentThread()) {reentrantLock1.unlock();moreReentrantLock.remove(resource);}}}static class _ReentrantLock {private ReentrantLock reentrantLock;private String resource;public ReentrantLock getReentrantLock() {return reentrantLock;}public void setReentrantLock(ReentrantLock reentrantLock) {this.reentrantLock = reentrantLock;}public String getResource() {return resource;}public void setResource(String resource) {this.resource = resource;}public _ReentrantLock(ReentrantLock reentrantLock, String resource) {this.reentrantLock = reentrantLock;this.resource = resource;}// equals hashcode 进行重新  判断锁对象是否为同一个 使用resource 进行判断@Overridepublic boolean equals(Object o) {if (this == o) {return true;}if (o == null || getClass() != o.getClass()) {return false;}_ReentrantLock that = (_ReentrantLock) o;return resource.equals(that.resource);}@Overridepublic int hashCode() {return Objects.hash(resource);}}
}

更多推荐

ReentrantLock的使用。

本文发布于:2023-07-28 18:00:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1269630.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ReentrantLock

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!