博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java练习题
阅读量:7016 次
发布时间:2019-06-28

本文共 1579 字,大约阅读时间需要 5 分钟。

1.实现一个类似于ConcurrentHashMap的分段加锁

import java.util.HashMap;import java.util.Map;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class SegmentLock {	private static final int DEFAULT_INITIAL_CAPACITY = 16;	private static final int MAXIMUM_CAPACITY = 1 << 30;	private static final int MAX_SEGMENTS = 1 << 16; // slightly conservative	final int segmentMask;	final int segmentShift;	private Pair
>[] segmentLocks; public SegmentLock(final int concurrentLevel) { int size = 1; int shift = 0; while (size < concurrentLevel) { size <<= 1; shift += 1; } segmentMask = size; segmentShift = 32 - shift; for (int i = 0; i < size; i++) { segmentLocks[i] = new Pair(new ReentrantLock(), new HashMap
()); } } public Pair
> get(final String key) { return segmentLocks[(hash(key.hashCode()) >> segmentShift) & segmentMask]; } private static int hash(int h) { h += (h << 15) ^ 0xffffcd7d; h ^= (h >>> 10); h += (h << 3); h ^= (h >>> 6); h += (h << 2) + (h << 14); return h ^ (h >>> 16); } static class Pair
{ private final Left left; private final Right right; public Pair(final Left left, final Right right) { this.left = left; this.right = right; } } static class Counter { private int count; public int increaseAndGet() { return ++count; } public int decreaseAndGet() { return --count; } public int get() { return count; } }}

  

2.怎样初始化一个泛型数组?

   new ArrayList<String>[]();

 

转载于:https://www.cnblogs.com/wuxinliulei/p/5464707.html

你可能感兴趣的文章
微软MCITP系列课程(三)本地用户与组账户的管理
查看>>
foreman架构的引入2-安装前环境准备
查看>>
perl学习笔记(9)
查看>>
数据为本,洞悉安全
查看>>
软件级负载均衡器(LVS/HAProxy/Nginx)的特点简介和对比
查看>>
rpm包指定安装路径
查看>>
AIX5.3配置使用Xmanager图形化连接案例
查看>>
mysql字符集调整总结
查看>>
zabbix 自定义key类型之计算(Calculated items)
查看>>
Cisco网络设备安全管理和报告
查看>>
易宝典文章——用ISA 2006标准版发布Exchange 2010的OWA系列之申请Excha
查看>>
MongoDB索引文件破坏后导致查询错误的问题
查看>>
99%运维人都需要的Linux命令大全
查看>>
一个月亮和 二 x 六个便士 ——2017年StatLee年度总结
查看>>
cocos2d-x学习笔记04:简单动画
查看>>
国企如何发展自己的信息化队伍
查看>>
Snort规则分析举例
查看>>
人们需要这样的智能手表
查看>>
Percona Xtrabackup快速备份MySQL
查看>>
NET Framework 类库 String.IsNullOrEmpty 方法 .
查看>>