Java并发编程-33-线程安全的可遍历映射
2015-06-21 11:04
495 查看
一、ConcurrentNavigableMap
这个接口的类用一下两个部分存放元素
一个键值,他是元素的唯一标识
元素的其他部分数据
二、ConcurrentSkipListMap
这个类内部使用了Skip List来存放数据,Skip List是基于并发列表的数据结构,效率与二叉树相近
参考这里
三、测试
package com.concurrencycollection;
/**
* 实体类,保存的是联系人信息
* @author Nicholas
*
*/
public class Contact {
private String name;
private String phone;
public Contact(String name, String phone) {
this.name = name;
this.phone = phone;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
@Override
public String toString() {
return "Contact [name=" + name + ", phone=" + phone + "]";
}
}
这个接口的类用一下两个部分存放元素
一个键值,他是元素的唯一标识
元素的其他部分数据
二、ConcurrentSkipListMap
这个类内部使用了Skip List来存放数据,Skip List是基于并发列表的数据结构,效率与二叉树相近
参考这里
三、测试
package com.concurrencycollection;
/**
* 实体类,保存的是联系人信息
* @author Nicholas
*
*/
public class Contact {
private String name;
private String phone;
public Contact(String name, String phone) {
this.name = name;
this.phone = phone;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
@Override
public String toString() {
return "Contact [name=" + name + ", phone=" + phone + "]";
}
}
package com.concurrencycollection; import java.util.concurrent.ConcurrentSkipListMap; public class Task implements Runnable { private ConcurrentSkipListMap<String, Contact> concurrentSkipListMap; private String id; public Task(ConcurrentSkipListMap<String, Contact> concurrentSkipListMap, String id) { this.concurrentSkipListMap = concurrentSkipListMap; this.id = id; } @Override public void run() { for (int i = 0; i < 1000; i++) { Contact contact = new Contact(id, String.valueOf(i + 1000)); concurrentSkipListMap.put(id + contact.getPhone(), contact); } } }
public void testContact() { ConcurrentSkipListMap<String, Contact> concurrentSkipListMap = new ConcurrentSkipListMap<String, Contact>(); Thread[] threads = new Thread[25]; int counter = 0; for (char i = 'A'; i < 'Z'; i++) { Task task = new Task(concurrentSkipListMap, String.valueOf(i)); threads[counter] = new Thread(task); threads[counter].start(); counter++; } for (int i = 0; i < threads.length; i++) { try { threads[i].join(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("Main : Size of the map " + concurrentSkipListMap.size()); Map.Entry<String, Contact> elementEntry; Contact contact; elementEntry = concurrentSkipListMap.firstEntry(); contact = elementEntry.getValue(); System.out.println("Main : First Entry :" + contact); elementEntry = concurrentSkipListMap.lastEntry(); contact = elementEntry.getValue(); System.out.println("Main : Last Entry :" + contact); // 取得子映射 System.out.println("Main : submap from A1996 to B1002:"); ConcurrentNavigableMap<String, Contact> subMap = concurrentSkipListMap .subMap("A1996", "B1002"); do { elementEntry = subMap.pollFirstEntry(); if (elementEntry != null) { contact = elementEntry.getValue(); System.out.println("Contact : " + contact); } } while (elementEntry != null); }
相关文章推荐
- 配置Spring AOP,XML方式比Annotation方式略好
- CXF(2.7.10) - Writing a service with Spring
- java_jdbc_1
- java NIO原理分析
- Struts2中的ModelDriven运用
- 深入理解Java对象序列化
- JVM系列文章(一):Java内存区域分析
- json for java入门总结
- Java中的clone方法详解
- 启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法!
- 深入理解Java对象序列化
- Struts2(五)自定义拦截器
- 【java】itoo项目实战之java Web 中web的作用
- Java--Reflect(反射)专题1——class 类的使用
- org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML doc
- 重踏学习Java路上_Day06(java 基础下与面向对象,二维数组与面向对象基础)
- (第五章)java面向对象之this的作用总结
- hadoop学习(7)----将Hadoop2.6源码导入到win7下的eclipse
- java编程大题
- java 易混淆知识点