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

猜你喜欢

转载自www.cnblogs.com/JuncaiF/p/11426101.html