java并发初探ConcurrentSkipListMap
java并发初探ConcurrentSkipListMap
ConcurrentSkipListMap以调表这种数据结构以空间换时间获得效率,通过volatile和CAS操作保证线程安全,而且它保证了有序性,比TreeMap比线程安全。
跳表结构
通过level down right可以更快插入和查找元素
*
* Head nodes Index nodes
* +-+ right +-+ +-+
* |2|---------------->| |--------------------->| |->null
* +-+ +-+ +-+
* | down | |
* v v v
* +-+ +-+ +-+ +-+ +-+ +-+
* |1|----------->| |->| |------>| |----------->| |------>| |->null
* +-+ +-+ +-+ +-+ +-+ +-+
* v | | | | |
* Nodes next v v v v v
* +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+
* | |->|A|->|B|->|C|->|D|->|E|->|F|->|G|->|H|->|I|->|J|->|K|->null
* +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+
例子
package com.java.javabase.thread.collection;
import lombok.extern.slf4j.Slf4j;
import java.security.SecureRandom;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
/**
* @author
*/
@Slf4j
public class ConcurrentSkipMapTest {
//public static TreeMap<String, Integer> map = new TreeMap();
public static ConcurrentSkipListMap<String, Integer> map = new ConcurrentSkipListMap<>();
public static int size = 10;
public static void main(String[] args) {
InnerThread t1 =new InnerThread("t1");
InnerThread t2 =new InnerThread("t2");
t1.start();
t2.start();
try {
Thread.sleep(1000);
printMap(map);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
static class InnerThread extends Thread {
String name;
public InnerThread(String name) {
super(name);
this.name=name;
}
@Override
public void run() {
Random random = new Random(System.currentTimeMillis());
for (int i = 0; i < size; i++) {
String key =String.valueOf(random.nextInt(1000))+name;
//Wlog.info(key);
map.put(key, Integer.valueOf(i));
printMapNone(map);
}
}
}
public static void printMap(Map<String, Integer> map) {
//Iterator<Map.Entry<K,V>> i = entrySet().iterator();
Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Integer> entry = it.next();
String key = entry.getKey();
Integer value = entry.getValue();
log.info("key {} value {}", key, value);
}
}
public static void printMapNone(Map<String, Integer> map) {
//Iterator<Map.Entry<K,V>> i = entrySet().iterator();
Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Integer> entry = it.next();
String key = entry.getKey();
Integer value = entry.getValue();
}
}
}
run result
019-08-28 19:34:31,919 [main] INFO ConcurrentSkipMapTest - key 101t1 value 5
2019-08-28 19:34:31,921 [main] INFO ConcurrentSkipMapTest - key 101t2 value 5
2019-08-28 19:34:31,921 [main] INFO ConcurrentSkipMapTest - key 131t1 value 7
2019-08-28 19:34:31,921 [main] INFO ConcurrentSkipMapTest - key 131t2 value 7
2019-08-28 19:34:31,922 [main] INFO ConcurrentSkipMapTest - key 180t1 value 3
2019-08-28 19:34:31,922 [main] INFO ConcurrentSkipMapTest - key 180t2 value 3
2019-08-28 19:34:31,922 [main] INFO ConcurrentSkipMapTest - key 209t1 value 9
2019-08-28 19:34:31,922 [main] INFO ConcurrentSkipMapTest - key 209t2 value 9
2019-08-28 19:34:31,922 [main] INFO ConcurrentSkipMapTest - key 349t1 value 6
2019-08-28 19:34:31,922 [main] INFO ConcurrentSkipMapTest - key 349t2 value 6
2019-08-28 19:34:31,923 [main] INFO ConcurrentSkipMapTest - key 527t1 value 4
2019-08-28 19:34:31,923 [main] INFO ConcurrentSkipMapTest - key 527t2 value 4
2019-08-28 19:34:31,923 [main] INFO ConcurrentSkipMapTest - key 655t1 value 1
2019-08-28 19:34:31,923 [main] INFO ConcurrentSkipMapTest - key 655t2 value 1
2019-08-28 19:34:31,923 [main] INFO ConcurrentSkipMapTest - key 714t1 value 8
2019-08-28 19:34:31,923 [main] INFO ConcurrentSkipMapTest - key 714t2 value 8
2019-08-28 19:34:31,923 [main] INFO ConcurrentSkipMapTest - key 781t1 value 2
2019-08-28 19:34:31,924 [main] INFO ConcurrentSkipMapTest - key 781t2 value 2
2019-08-28 19:34:31,924 [main] INFO ConcurrentSkipMapTest - key 797t1 value 0
2019-08-28 19:34:31,924 [main] INFO ConcurrentSkipMapTest - key 797t2 value 0
java并发初探ConcurrentSkipListMap的更多相关文章
- java并发初探ConcurrentHashMap
java并发初探ConcurrentHashMap Doug Lea在java并发上创造了不可磨灭的功劳,ConcurrentHashMap体现这位大师的非凡能力. 1.8中ConcurrentHas ...
- java并发初探ThreadPoolExecutor拒绝策略
java并发初探ThreadPoolExecutor拒绝策略 ThreadPoolExecuter构造器 corePoolSize是核心线程池,就是常驻线程池数量: maximumPoolSize是最 ...
- java并发初探CyclicBarrier
java并发初探CyclicBarrier CyclicBarrier的作用 CyclicBarrier,"循环屏障"的作用就是一系列的线程等待直至达到屏障的"瓶颈点&q ...
- java并发初探CountDownLatch
java并发初探CountDownLatch CountDownLatch是同步工具类能够允许一个或者多个线程等待直到其他线程完成操作. 当前前程A调用CountDownLatch的await方法进入 ...
- java并发初探ReentrantWriteReadLock
java并发初探ReentrantWriteReadLock ReenWriteReadLock类的优秀博客 ReentrantReadWriteLock读写锁详解 Java多线程系列--" ...
- Java并发指南14:Java并发容器ConcurrentSkipListMap与CopyOnWriteArrayList
原文出处http://cmsblogs.com/ 『chenssy』 到目前为止,我们在Java世界里看到了两种实现key-value的数据结构:Hash.TreeMap,这两种数据结构各自都有着优缺 ...
- Java并发容器——ConcurrentSkipListMap和ConcurrentHashMap
一:ConcurrentSkipListMap TreeMap使用红黑树按照key的顺序(自然顺序.自定义顺序)来使得键值对有序存储,但是只能在单线程下安全使用:多线程下想要使键值对按照key的顺序来 ...
- java并发:初探sleep方法
sleep与wait sleep是Thread方法,使得当前线程从运行态变为阻塞态.但它不会释放对象的锁. wait方法是Object方法,它的作用是使得当前拥有对象锁的线程从运行态变为阻塞态, 它会 ...
- java并发队列
阻塞队列 常见的阻塞队列有ArrayBlockingQueue,LinkedBlockingDeque,LinkedBlockingQueue,这些队列有界且可以阻塞线程 ArrayBlockingQ ...
随机推荐
- concat merge
# concat import numpy as np import pandas as pd from pandas import Series,DataFrame df1 = DataFrame( ...
- EFCore.BulkExtensions Demo
最近做了一个项目,当用EF传统的方法执行时,花时4小时左右,修改后,时间大大减少到10分钟,下面是DEMO实例 实体代码: public class UserInfoEntity { [Key] pu ...
- awk函数实现将点分式形式的掩码转换为十进制形式的掩码
在用awk处理文本时,需要将源数据为点分式形式的掩码转换为十进制形式的掩码,下边的函数可简单实现: 转换功能函数cdr2mask function cdr2mask(mask_point) { num ...
- Python学习之面向对象基础
python的面向对象和以前学的c++,Java都是一般,大同小异,面向对象基础先谈谈类的构造,编写,属性和方法的可见性等等 1.定义类,创建和使用对象 #定义类 class Student(obje ...
- 解决tensorflow Saver.restore()无效的问题
解决tensorflow 的 Saver.restore()无法从本地读取变量的问题 最近做tensorflow 手写数字识别的时候遇到了一个问题,Saver的restore()方法无法从本地恢复变量 ...
- 吴裕雄 python 神经网络——TensorFlow 完整神经网络样例程序
import tensorflow as tf from numpy.random import RandomState batch_size = 8 w1= tf.Variable(tf.rando ...
- C++标准库里面没有字符分割函数split,自己编写函数实现字符串分割功能
#include <vector> #include <string> #include <iostream> using namespace std; vecto ...
- 第八届极客大挑战 Re
0x01.Writeup-RE-CM_2 题目: 解题思路: 1.这个是经过xor的,王老师提示说用xortool,于是放进kali,装好之后执行 xortool CM_2.exe -b, 0.out ...
- java linux安装jdk,git, maven
jdk8下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html mkdir ...
- Spring Boot 使用 CXF 调用 WebService 服务
上一张我们讲到 Spring Boot 开发 WebService 服务,本章研究基于 CXF 调用 WebService.另外本来想写一篇 xfire 作为 client 端来调用 webservi ...