1 Star 3 Fork 0

清风/CommonUtils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
AsyncMonitorTmpContainer.java 2.31 KB
一键复制 编辑 原始数据 按行查看 历史
清风 提交于 2019-08-16 11:19 . Update AsyncMonitorTmpContainer.java
import lombok.extern.slf4j.Slf4j;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;
@Slf4j
public class AsyncMonitorTmpContainer<K,V> extends LRUCacheUtils<K,V> {
private static final ReentrantLock lockPut =new ReentrantLock();
private static final ReentrantLock lockRemove =new ReentrantLock();
public AsyncMonitorTmpContainer(){
}
public AsyncMonitorTmpContainer(int capacity){
super(capacity);
}
@Override
public boolean containsKey(Object key) {
return super.containsKey(key);
}
@Override
public V getOrDefault(Object key, V defaultValue) {
return super.getOrDefault(key, defaultValue);
}
@Override
public V put(K key, V value) {
if (key==null || value==null) return null;
lockPut.lock();
V put=null;
try {
put = super.put(key, value);
}catch (Exception e){
}finally {
lockPut.unlock();
}
return put;
}
@Override
public V remove(Object key) {
lockRemove.lock();
if (key==null) return null;
V remove=null;
try {
remove = super.remove(key);
}catch (Exception e){
}finally {
lockRemove.unlock();
}
return remove;
}
public Set<K> getList(){
return this.keySet();
}
public static void main(String[] args) {
AsyncMonitorTmpContainer<Integer,Integer> container=new AsyncMonitorTmpContainer();
Random random=new Random();
for (int i=0;i<100;++i){
new Thread(){
public void run(){
for (int j=0;j<10000;j++){
Integer key=random.nextInt(5000);
Integer v=random.nextInt(5000);
container.put(key,v);
}
}
}.start();
new Thread(){
public void run(){
for (int j=0;j<5000;j++){
Integer key=random.nextInt(5000);
container.remove(key);
if (container.size()>500)
System.out.println(container.size());
}
}
}.start();
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/agp5050/CommonUtils.git
[email protected]:agp5050/CommonUtils.git
agp5050
CommonUtils
CommonUtils
master

搜索帮助