基于 equals() 的 键值同步锁: generic key managed concurrent lock map

本文介绍了一个利用equals方法实现的键值同步锁工具类KeyMap,旨在通过特定类型键进行锁定,确保数据的安全访问。灵感来源于惑惑的博客,提供了一种全局单例类的实现方式,用于根据类型键进行线程安全的并发控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


用户可以根据需要建立自己的全局单例类,而把这个 KeyMap 类作为背后的工具类使用,达到根据某类型的键来锁定保护数据的目的。


最初的设计来源于惑惑的博客 http://blog.csdn.net/icebamboo_moyun/article/details/9391915

感谢他的无私分享。


/*
 * Copyright 2013 (raistlic@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

/**
 * This class allows multiple threads to lock against a key of the specified type, 
 * based on the {@code equals()} contract defined in {@link java.lang.Object} 
 * class, in a reentrant manner.
 * 
 * <p/>
 * All methods defined in this class, including the static factory methods, are 
 * thread safe.
 * 
 * @param <K> the specified key type
 * 
 * @author icebamboo_moyun, raistlic
 */
public abstract class LockMap<K> {
  
  /**
   * This method creates and exports a {@code LockMap} instance that is "Garbage
   * Collector friendly", that is, it releases any lock references that is no 
   * longer used. It is recomm
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值