3

The following class acts as a simple cache that gets updated very infrequently (say e.g. twice a day) and gets read quite a lot (up to several times a second). There are two different types, a List and a Map. My question is about the new assignment after the data gets updated in the update method. What's the best (safest) way for the new data to get applied?

I should add that it isn't necessary for readers to see the absolute latest value. The requirements are just to get either the old or the new value at any given time.

public class Foo {

    private ThreadPoolExecutor _executor;
    private List<Object> _listObjects = new ArrayList<Object>(0);
    private Map<Integer, Object> _mapObjects = new HashMap<Integer, Object>();
    private Object _mutex = new Object();
    private boolean _updateInProgress;

    public void update() {

        synchronized (_mutex) {
            if (_updateInProgress) {
                return;
            } else {
                _updateInProgress = true;
            }
        }

        _executor.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    List<Object> newObjects = loadListObjectsFromDatabase();
                    Map<Integer, Object> newMapObjects = loadMapObjectsFromDatabase();

                    /*
                     * this is the interesting part
                     */
                    _listObjects = newObjects;
                    _mapObjects = newMapObjects;

                } catch (final Exception ex) {
                    // error handling
                } finally {
                    synchronized (_mutex) {
                        _updateInProgress = false;
                    }
                }
            }
        });
    }

    public Object getObjectById(Integer id) {
        return _mapObjects.get(id);
    }

    public List<Object> getListObjects() {
        return new ArrayList<Object>(_listObjects);
    }


}

As you see, currently no ConcurrentHashMap or CopyOnWriteArrayList is used. The only synchronisation is done in the update method.

Although not necessary for my current problem, it would be also great to know the best solution for cases where it is essential for readers to always get the absolute latest value.

4

2 に答える 2

1

You could use plan synchronization unless you are reading over 10,000 times per second.

If you want concurrent access I would use on of the concurrent collections like ConcurrentHashMap or CopyOnWriteArrayList. These are simpler to use than synchronizing the collection. (i.e. you don't need them for performance reasons, use them for simplicity)

BTW: A modern CPU can perform billions of operations in 0.1 seconds so several times a second is an eternity to a computer.

于 2012-06-19T10:34:13.707 に答える
0

I am also seeing this issue and think of multiple solutions:

  1. Use synchronization block on the both codes, one where reading and other where writing.
  2. Make a separate remove list, add all removable items in that list. Remove in the same thread where reading the list just after reading is done. This way reading and deleting will happen in sequence and no error will come.
于 2012-09-12T12:57:18.630 に答える