- 浏览: 364522 次
- 性别:
- 来自: Alien
文章分类
最新评论
-
风一样的男人__:
[flash=200,200][url]引用[/url][/f ...
java线程内存模型,线程、工作内存、主内存 -
极乐君:
厉害了,,,请问可以转载到我们专栏吗?会注明来源和地址的~专栏 ...
java线程内存模型,线程、工作内存、主内存 -
zdd001:
Spring 线程池使用 -
zdd001:
Spring 线程池使用 -
u014663756:
看了三行就知道是我想要的!!
java线程内存模型,线程、工作内存、主内存
ConcurrentSkipListSet
ConcurrentSkipListSet(在JavaSE 6新增的)提供的功能类似于TreeSet,能够并发的访问有序的set。因为ConcurrentSkipListSet是基于“跳跃列表(skip list)”实现的,只要多个线程没有同时修改集合的同一个部分,那么在正常读、写集合的操作中不会出现竞争现象。
有一个业务需求,需要对用户进行增删查改的操作。
当用户第一次查询时会把所以的数据都从数据库当中查询出来。放入cache当中。
如果有增加时,会把这个新用用户放入并行集合类ConcurrentSkipListSet 当中。
删除时,数据库删除后会把这个用户信息从ConcurrentSkipListSet 中删除。
修改时。会从ConcurrentSkipListSet 中根据ID找到这个数据。然后删除。然后ADD进去。
/**
*
* @author matt.yan
*/
package uma.admin.cache;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.regex.Pattern;
import uma.admin.ws.User;
import uma.admin.ws.UserAccount;
import uma.ws.model.SourceModel.Source;
import uma.ws.model.UserAccountModel;
import uma.ws.model.UserAccountModelList;
import uma.ws.model.UserModel;
import uma.ws.model.UserModelList;
public class UserCache {
private static final UserCache usersCache = new UserCache();
private static volatile ConcurrentSkipListSet<UserModel> models = null;
public static UserCache getUsersCache() throws Exception {
if (models == null) {
synchronized (UserCache.class) {
if (models == null) {
UserModelList list = User.usersGet();
models = new ConcurrentSkipListSet<UserModel> (
new Comparator<UserModel>() {
@Override
public int compare(UserModel o1, UserModel o2) {
if(o1.getLastName() == null && o1.getFirstName() == null) {
return -1;
}
if(o2.getLastName() == null && o2.getFirstName() == null) {
return 1;
}
if(o1.getLastName() == null || o2.getLastName() == null) {
return o1.getLastName() == null ? -1 : 1;
}
int compare = o1.getLastName().trim().toUpperCase().compareTo(o2.getLastName().trim().toUpperCase());
if(compare == 0){
if(o1.getFirstName() == null || o1.getFirstName() == null){
return o1.getLastName() == null ? -1 : 1;
}
compare = o1.getFirstName().trim().toUpperCase().compareTo(o2.getFirstName().trim().toUpperCase());
}
return compare;
}
});
for(UserModel userModel : list){
models.add(userModel);
}
}
}
}
return usersCache;
}
public List getModels(String search, long searchRoleId, int searchStatusId) throws Exception {
ConcurrentSkipListSet<UserModel> localSet = models;
if (localSet == null) {
return null;
}
if ((search == null || search.isEmpty()) && searchRoleId < 0 && searchStatusId < 0) {
return Arrays.asList(localSet.toArray());
}
List list = new UserModelList();
Iterator it = localSet.iterator();
if (search == null || search.isEmpty()) {
while (it.hasNext()) {
UserModel user = (UserModel) it.next();
if ((user.getRoleId() == searchRoleId || searchRoleId < 0) &&
(user.getStatus() == searchStatusId || searchStatusId < 0)) {
list.add(user);
}
}
}
else {
search = search.toLowerCase().trim();
Pattern integerPattern = Pattern.compile("^\\d*$");
//integer will search pams id
UserAccountModelList userAccounts = null;
if (integerPattern.matcher(search).matches()) {
userAccounts = UserAccount.getUserAccountsByReference(Source.PAMS.toString(), search);
}
while (it.hasNext()) {
UserModel user = (UserModel) it.next();
//normally this list will be no more than 10 records
if(userAccounts != null) {
for(UserAccountModel userAccount : userAccounts){
if((userAccount.getUserId() == user.getId())
&& (user.getRoleId() == searchRoleId || searchRoleId < 0) &&
(user.getStatus() == searchStatusId || searchStatusId < 0)){
list.add(user);
continue;
}
}
}
String userId = new Long(user.getId()).toString();
if (((userId != null && userId.contains(search)) ||
(user.getEmail() != null && user.getEmail().toLowerCase().contains(search)) ||
(user.getFirstName() != null && user.getFirstName().toLowerCase().contains(search)) ||
(user.getLastName() != null && user.getLastName().toLowerCase().contains(search)) ||
(user.getLoginName() != null && user.getLoginName().toLowerCase().contains(search))
)
&& (user.getRoleId() == searchRoleId || searchRoleId < 0) &&
(user.getStatus() == searchStatusId || searchStatusId < 0))
{
list.add(user);
}
}
}
return list;
}
/*
* this operation will not be heavily used
*/
public UserModel addModel(UserModel user) throws Exception {
if(user == null){
return null;
}
user = User.userCreate(user);
if(user != null){
ConcurrentSkipListSet<UserModel> localSet = models;
localSet.add(user);
}
return user;
}
/*
* this operation will not be heavily used
*/
public UserModel updateModel(UserModel user) throws Exception {
if(user == null){
return null;
}
user = User.userUpdate(user);
put(user);
return user;
}
/*
* Password format:
* [aaabbbn] where:
* aaa = first three characters of client’s first name and
* bbbb = first three characters of client’s surname, and
* n = suffix number – this is only required where the ‘a’ and ‘b’ combination already exists. Starting at 1, continue until a unique combination is found.
* This function is only used in creating a user
*/
public String getLoginName(String firstName, String lastName) {
if(firstName == null || lastName == null
|| firstName.isEmpty() || lastName.isEmpty()) {
return null;
}
if (firstName.length()>3) {
firstName = firstName.substring(0, 3);
}
if (lastName.length()>3) {
lastName = lastName.substring(0, 3);
}
firstName = firstName.toLowerCase();
lastName = lastName.toLowerCase();
String loginName = firstName + lastName;
int length = loginName.length();
for(int i = length; i<6 ; i++) {
loginName = loginName + "1";
}
ConcurrentSkipListSet<UserModel> localSet = models;
int largestId =0;
String matchReg = "^" + loginName + "[0-9]*";
for(UserModel user : localSet) {
if(user.getLoginName() != null && user.getLoginName().toLowerCase().matches(matchReg)) {
int i = Integer.parseInt(user.getLoginName().substring(6));
if (largestId < i) {
largestId = i;
}
}
}
largestId++;
return loginName + largestId;
}
/*
* this operation will not be heavily used
*/
private void put(UserModel wsUser) {
ConcurrentSkipListSet<UserModel> localSet = models;
if(wsUser == null || localSet == null){
return;
}
UserModel cacheUser = null;
for(UserModel user : localSet){
if(user.getId() == wsUser.getId()) {
cacheUser = user;
break;
}
}
synchronized (UserCache.class) {
if(cacheUser != null){
localSet.remove(cacheUser);
}
localSet.add(wsUser);
}
}
/*
* this operation will not be heavily used
*/
public UserModel getUserById(long id) throws Exception {
UserModel wsUser = User.userGet(id);
put(wsUser);
return wsUser;
}
}
发表评论
-
Spring 线程池使用2
2013-04-25 18:41 1108<!-- 配置异步线程执行器 --> < ... -
java synchronized与lock区别 转
2013-03-20 01:59 1012java synchronized与lock区别 ... -
java并发编程实践笔记(转)
2012-11-28 01:14 900一、 保证线程安全的 ... -
Amdahl 定律 (阿姆达尔定律)
2012-11-24 02:14 2027有些问题使用越多的资源就能越快地解决——越多的工人参 ... -
java线程内存模型,线程、工作内存、主内存
2012-11-24 02:08 18097java线程内存模型 线程、工作内存、主内存三者之间的 ... -
线程同步与异步
2012-11-22 23:53 2436Java中的线程同步与 ... -
Java 并发核心编程(转)
2012-11-06 10:16 977Java 并发核心编程(转) Java 并发核心编程 ... -
java线程:关于Callable和Future的简单使用:有返回值的线程
2012-11-06 09:19 1923分类: java_线程2012-07-03 19 ... -
java数据结构:ConcurrentSkipListSet<E>与CopyOnWriteArraySet<E>
2012-11-06 09:20 5796分类: java_数据结构 java_线程201 ... -
java线程:信号量
2012-11-06 09:20 840Java 信号量 Semaphore 介绍 Semaphore ... -
java线程:一个死锁的小程序
2012-11-06 09:20 2678死锁是这样一种 ... -
java线程:互斥锁与读写锁
2012-11-05 17:34 6998java线程:互斥锁与读写锁 分类: ja ... -
java线程:三种方式实现生产者消费者问题_3
2012-11-05 17:34 1275java线程:三种方式 ... -
java线程:几个实用的线程工具类
2012-11-05 15:15 8322java线程:几个实用的线程工具类 分类: ... -
java线程:三种方式实现生产者消费者问题_2
2012-11-05 14:26 1084java线程:三种方式实现生产者消费者问题_2 ... -
java线程:三种方式实现生产者消费者问题_1
2012-11-05 14:22 1124java线程:三种方式实现生产者消费者问题_1 ... -
java ExecutorService 线程池Callable 和 Future接口
2012-08-29 21:10 1108javaEye转载(java多线程 Callable 的实现) ... -
线程并发
2012-03-31 15:41 10111:>内存模型 究竟什么是内存模型?内存模型描述了程序中 ... -
特殊的队列:BlockingQueue 使用(转)
2012-03-28 14:49 1023本例介绍一个特殊的队列:BlockingQueue,如果Blo ... -
Spring 线程池使用
2012-03-28 12:47 26043Spring 线程池使用 /** * */ 一个日志管理 ...
相关推荐
ConcurrentSkipListSet 是Java中的一个线程安全的有序集合类,它基于跳表(Skip List)数据结构实现。 下面是关于 ConcurrentSkipListSet 的一些重要信息: 线程安全性:ConcurrentSkipListSet 是线程安全的,可以...
主要为大家详细介绍了Java concurrency集合之ConcurrentSkipListSet的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
第二章 线程的并发工具类 21 一、 Fork/Join框架的介绍 21 1、实现步骤: 22 2、工作窃取算法 22 3、分而治之 23 4、Fork/Join使用的标准范式 24 5、Fork/Join框架的异常处理 26 6、Fork/Join框架的实现原理 26 二、...
ConcurrentSkipListSet 替代同步的SortedSet Java 5 Java 6 同步工具类 闭锁 *应用场景 (1)确保某个计算在其需要的所有资源都被初始化后才能继续执行 (2)确保某个服务...
2.HashSet,TreeSet -- CopyONWriteArraySet,ConcurrentSkipListSet 3.hashMap , treeMap -- ConcurrentHashMap,ConcurentSkipListMap(key有序,支持更高并发) ## Concurrent同步工具类 countDownLatch ...
以循环1000000万次为标准! 定义如下数组 public static char[] chars = {'A','B','C'...ConcurrentSkipListSet , CopyOnWriteArraySet , EnumSet…, HashSet JobStateReasons , LinkedHashSet , TreeSet Map接口 …….
线程安全set:ConcurrentSkipListSet、CopyOnWriteArraySet、ConcurrentHashMap.newKeySet()、Collections.synchronizedSet(set) 分布式技术 缓存穿透: 请求去查询缓存数据库中根本就不存在的数据。解决方案: 缓存...