From 35eea73b2b9e381efd5da60662f0f64f59b91406 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 21 Jul 2016 17:39:04 +0500 Subject: Act with atomic more carefully --- src/org/traccar/database/DeviceManager.java | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index 916e7fe67..f442dfb19 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -43,10 +43,10 @@ public class DeviceManager implements IdentityManager { private Map devicesById; private Map devicesByUniqueId; - private AtomicLong devicesLastUpdate = new AtomicLong(0); + private AtomicLong devicesLastUpdate = new AtomicLong(); private Map groupsById; - private AtomicLong groupsLastUpdate = new AtomicLong(0); + private AtomicLong groupsLastUpdate = new AtomicLong(); private final Map positions = new ConcurrentHashMap<>(); @@ -68,8 +68,10 @@ public class DeviceManager implements IdentityManager { } private void updateDeviceCache(boolean force) throws SQLException { - boolean needWrite = force || System.currentTimeMillis() - devicesLastUpdate.get() > dataRefreshDelay; - if (needWrite) { + + long lastUpdate = devicesLastUpdate.get(); + if (force || System.currentTimeMillis() - lastUpdate > dataRefreshDelay + && devicesLastUpdate.compareAndSet(lastUpdate, System.currentTimeMillis())) { GeofenceManager geofenceManager = Context.getGeofenceManager(); Collection databaseDevices = dataManager.getAllDevices(); if (devicesById == null) { @@ -79,10 +81,10 @@ public class DeviceManager implements IdentityManager { devicesByUniqueId = new ConcurrentHashMap<>(databaseDevices.size()); } Set databaseDevicesIds = new HashSet<>(); - Set databaseDevicesUniqIds = new HashSet<>(); + Set databaseDevicesUniqueIds = new HashSet<>(); for (Device device : databaseDevices) { databaseDevicesIds.add(device.getId()); - databaseDevicesUniqIds.add(device.getUniqueId()); + databaseDevicesUniqueIds.add(device.getUniqueId()); if (devicesById.containsKey(device.getId())) { Device cachedDevice = devicesById.get(device.getId()); cachedDevice.setName(device.getName()); @@ -112,13 +114,12 @@ public class DeviceManager implements IdentityManager { } } for (String cachedDeviceUniqId : devicesByUniqueId.keySet()) { - if (!databaseDevicesUniqIds.contains(cachedDeviceUniqId)) { + if (!databaseDevicesUniqueIds.contains(cachedDeviceUniqId)) { devicesByUniqueId.remove(cachedDeviceUniqId); } } databaseDevicesIds.clear(); - databaseDevicesUniqIds.clear(); - devicesLastUpdate.set(System.currentTimeMillis()); + databaseDevicesUniqueIds.clear(); } } @@ -230,9 +231,10 @@ public class DeviceManager implements IdentityManager { } private void updateGroupCache(boolean force) throws SQLException { - boolean needWrite = force || System.currentTimeMillis() - groupsLastUpdate.get() > dataRefreshDelay; - if (needWrite) { + long lastUpdate = groupsLastUpdate.get(); + if (force || System.currentTimeMillis() - lastUpdate > dataRefreshDelay + && groupsLastUpdate.compareAndSet(lastUpdate, System.currentTimeMillis())) { Collection databaseGroups = dataManager.getAllGroups(); if (groupsById == null) { groupsById = new ConcurrentHashMap<>(databaseGroups.size()); @@ -254,7 +256,6 @@ public class DeviceManager implements IdentityManager { } } databaseGroupsIds.clear(); - groupsLastUpdate.set(System.currentTimeMillis()); } } -- cgit v1.2.3