提问者:小点点

Java并发HashMap迭代


我正在使用一个线程(让我们称它为“MapChecker”),它在其整个生命周期内在并发HashMap上循环。

映射从其他线程填充,并由MapChecker通过使用迭代器对其进行清除。

该地图具有以下结构:

private volatile Map<MyObject, SynchronizedList<MyOtherObject>> map = new ConcurrentHashMap<>();
//SynchronizedList = Collections.syncrhonizedList.

MapChecker必须更新其循环中每个键的值。更新是通过从列表中删除元素或删除完整的映射条目来进行的。

同步分两个步骤进行:

  1. 在地图内部添加数据时(同步)
  2. 检索地图的迭代器时(在MapChecker内同步)。

锁在映射本身(同步(map))上。

我不关心总是在我的迭代器视图中最后更新的值,但我需要确保所有丢失的值将在下一次迭代中检索到,这很重要,因为我不想跳过任何元素。此外,正确更新同步列表也很重要。

我的问题是:我可以确保通过这种架构插入/更新所有条目吗?是否有遗漏某些内容的风险?如果MapChecker删除一个条目,而其他线程正在更新相同的条目会发生什么?并发HashMap应该阻止这些操作,所以我不希望有任何麻烦。

这是MapChecker循环:

while (!isInterrupted()) {

    executeClearingPhases();

    Iterator<Map.Entry<PoolManager, List<PooledObject>>> it = null;
    synchronized (idleInstancesMap) {
        it = idleInstancesMap.entrySet().iterator();
    }

    while (it.hasNext()) {

        Map.Entry<PoolManager, List<PooledObject>> entry = it.next();
        PoolManager poolManager = entry.getKey();

        boolean stop = false;
        while (!stop) {
            //this list is empty very often but it shouldn't, that's the problem I am facing. I need to assure updates visibility.
            List<PooledObject> idlePooledObjects = entry.getValue();
            if (idlePooledObjects.isEmpty()) {

                stop = true;
            } else {

                PooledObject pooledObject = null;
                try {

                    pooledObject = idlePooledObjects.get(0);
                    info(loggingId, " - REMOOOVINNGG:  \"", pooledObject.getClientId(), "\".");
                    PoolingStatus destroyStatus = poolManager.destroyIfExpired(pooledObject);
                    switch (destroyStatus) {

                        case DESTROY:
                            info(loggingId, " - Removed pooled object \"", pooledObject.getClientId(), "\" from pool: \"", poolManager.getClientId(), "\".");
                            idlePooledObjects.remove(0);
                            break;
                        case IDLE:
                            stop = true;
                            break;
                        default:
                            idlePooledObjects.remove(0);
                            break;
                    }
                } catch (@SuppressWarnings("unused") PoolDestroyedException e) {

                    warn(loggingId, " - WARNING: Pooled object \"", pooledObject.getClientId(), "\" skipped, pool: \"", poolManager.getClientId(), "\" has been destroyed.");
                    synchronized(idleInstancesMap) {

                        it.remove();
                    }
                    stop = true;
                } catch (PoolManagementException e) {

                    error(e, loggingId, " - ERROR: Errors occurred during the operation.");
                    idlePooledObjects.remove(0);
                }
            }
        }
    }
    Thread.yield();
}

这是任何其他线程调用(多次)的方法:

public void addPooledObject(PoolManager poolManager, PooledObject pooledObject) {

        synchronized (idleInstancesMap) {

            List<PooledObject> idleInstances = idleInstancesMap.get(poolManager);
            if (idleInstances == null) {

                idleInstances = Collections.synchronizedList(new LinkedList<PooledObject>());
                idleInstancesMap.put(poolManager, idleInstances);
            }

            idleInstances.add(pooledObject);
        }
    }

谢啦


共2个答案

匿名用户

多亏了PatrickChen的建议,我在每个PoolManager中移动了PooledObject实例的列表(它已经拥有这个列表,因为它以完全同步的方式拥有池及其内部状态)。

这是结果:

//MapChecker lifecycle
public void run() {

    try {

        while (!isInterrupted()) {

            executeClearingPhases();

            ListIterator<PoolManager> it = null;
            //This really helps. poolManagers is the list of PoolManager instances.
            //It's unlikely that this list will have many elements (maybe not more than 20)
            synchronized (poolManagers) {

                Iterator<PoolManager> originalIt = poolManagers.iterator();

                while (originalIt.hasNext()) {

                    if (originalIt.next().isDestroyed()) {

                        originalIt.remove();
                    }
                }
                //This iterator will contain the current view of the list.
                //It will update on the next iteration.
                it = new LinkedList<PoolManager>(poolManagers).listIterator();
            }

            while (it.hasNext()) {

                PoolManager poolManager = it.next();
                try {
                    //This method will lock on its internal synchronized pool in order to
                    //scan for expired objects.
                    poolManager.destroyExpired();
                } catch (@SuppressWarnings("unused") PoolDestroyedException e) {

                    warn(loggingId, " - WARNING: Pool: \"", poolManager.getClientId(), "\" has been destroyed.");
                    it.remove();
                }
            }
            Thread.yield();
        }
        throw new InterruptedException();
    } catch (@SuppressWarnings("unused") InterruptedException e) {

        started = false;
        Thread.currentThread().interrupt();
        debug(loggingId, " - Pool checker interrupted.");
    }
}
//Method invoked by multiple threads
public void addPooledObject(PoolManager poolManager) {

    synchronized (poolManagers) {

        poolManagers.add(poolManager);
    }
}

匿名用户

但是我需要确保所有丢失的值都将在下一次迭代中检索到,这很重要,因为我不想跳过任何元素。

首先,我认为根据你的解决方案,我认为只要你继续执行MapChecker循环,你就会得到地图中的所有项目。我建议你在你呈现的MapChecker代码之外有一个额外的while(true)循环。

但是根据你所有的描述,我建议你应该使用Queue而不是Map,显然,你的问题需要一个push/pop操作,也许BlockingQueue更适合这里。