java - 如何正确使用concurrentskiplistmap?

标签 java concurrency java.util.concurrent concurrentskiplistmap

尝试使用并发跳过列表映射。我遇到了问题 how to use a synchronized linked hash map correctly ,所以我决定尝试并发跳过列表映射。

我也有同样的问题。下面的单元测试失败,因为当我获取条目集时,当 size() 指示映射不为空时,它具有空值。 naict,我可以同步访问 map 。

我认为不需要这样做(同步),因为这是一个并发 map 。

服务器只是将数字 0,1,2,3, ... 放入 map 中,使其大小保持在阈值以下。它尝试在服务器启动后的每一毫秒内输入一个数字。

任何指示将不胜感激。

谢谢

import static org.junit.Assert.*;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentSkipListMap;
import org.junit.*;
class DummyServer implements Runnable {
    DummyServer(int pieces) {
        t0=System.currentTimeMillis();
        this.pieces=pieces;
        max=pieces;
        lruMap=new ConcurrentSkipListMap<Long,Long>();
    }
    Set<Map.Entry<Long,Long>> entrySet() {
        Set<Entry<Long,Long>> entries=null;
        synchronized(lruMap) {
            entries=Collections.unmodifiableSet(lruMap.entrySet());
        }
        return entries;
    }
    Set<Long> keySet() {
        Set<Long> entries=null;
        synchronized(lruMap) {
            entries=Collections.unmodifiableSet(lruMap.keySet());
        }
        return entries;
    }
    @Override public void run() {
        int n=0;
        while(piece<stopAtPiece) {
            long target=piece(System.currentTimeMillis()-t0);
            long n0=piece;
            for(;piece<target;piece++,n++)
                put(piece);
            if(n>max+max/10) {
                Long[] keys=keySet().toArray(new Long[0]);
                synchronized(lruMap) {
                    for(int i=0;n>max;i++,n--)
                        lruMap.remove(keys[i]);
                }
            }
            try {
                Thread.sleep(10);
            } catch(InterruptedException e) {
                e.printStackTrace();
                break;
            }
        }
    }
    private void put(long piece) {
        synchronized(lruMap) {
            lruMap.put(piece,piece);
        }
    }
    public long piece() {
        return piece;
    }
    public Long get(long piece) {
        synchronized(lruMap) {
            return lruMap.get(piece);
        }
    }
    public int size() {
        synchronized(lruMap) {
            return lruMap.size();
        }
    }
    public long piece(long dt) {
        return dt/period*pieces+dt%period*pieces/period;
    }
    private long piece;
    int period=2000;
    private volatile Map<Long,Long> lruMap;
    public final long t0;
    protected final int pieces;
    public final int max;
    public long stopAtPiece=Long.MAX_VALUE;
}
public class DummyServerTestCase {
    void checkMap(Long n) {
        if(server.size()>0) {
            final Set<Map.Entry<Long,Long>> mapValues=server.entrySet();
            @SuppressWarnings("unchecked") final Map.Entry<Long,Long>[] entries=new Map.Entry[mapValues.size()];
            mapValues.toArray(entries);
            try {
                if(entries[0]==null)
                    System.out.println(server.piece());
                assertNotNull(entries[0]);
            } catch(Exception e) {
                fail(e.toString());
            }
        }
    }
    @Test public void testRunForFirstIsNotZero() {
        server.stopAtPiece=1*server.pieces;
        Thread thread=new Thread(server);
        thread.start();
        while(thread.isAlive()) {
            for(long i=0;i<server.piece();i++) {
                server.get(i);
                Thread.yield();
                checkMap(server.piece());
                Thread.yield();
            }
        }
    }
    DummyServer server=new DummyServer(1000);
}

最佳答案

问题在于你的表现

final Map.Entry<Long,Long>[] entries=new Map.Entry[mapValues.size()]; // size>0
mapValues.toArray(entries); // size is 0.

在创建数组和调用 toArray 之间,您正在清除 map 。

如果您使用迭代器获取副本,您将不会遇到此竞争条件。

void checkMap(Long n) {
    final Set<Map.Entry<Long, Long>> mapValues = server.entrySet();
    Set<Map.Entry<Long, Long>> entries = new LinkedHashSet<>(mapValues);

    for (Entry<Long, Long> entry : entries) {
        assertNotNull(entry);
    }
}

void checkMap(Long n) {
    for (Entry<Long, Long> entry : server.entrySet())
        assertNotNull(entry);
}

关于java - 如何正确使用concurrentskiplistmap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11937297/

相关文章:

java - 什么样的设计模式适合这里?

concurrency - CompletableFuture supplyAsync 与 Stream.map()

java - 寻找合适的同步机制

java主线程管理启动其他线程

java - 使用 CompletionService 时强制执行 executorService.awaitTermination

MySQL COMRESS DECOMPRESS 函数的 Java 模拟

java - 如何在数组列表中删除这种情况,而不是仅仅检查它是否发生

java - 在java中将两个相互递归方法转换为单个递归方法?

java - 当线程更改状态时,有什么方法可以在进程中获取通知吗?

java - ArrayList 上的 ConcurrentModificationException