aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/database/CommandsManager.java
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2020-02-22 11:34:57 -0800
committerAnton Tananaev <anton.tananaev@gmail.com>2020-02-22 11:34:57 -0800
commitdeb519ebd6798450509afaf4067e140edd7eb0d0 (patch)
treee93d94d51245355e2b43c3cb3aedc77e169f0f09 /src/main/java/org/traccar/database/CommandsManager.java
parentcb25fe3e58c0876fe6c000a1f0a7c7435e3fb0e1 (diff)
downloadtrackermap-server-deb519ebd6798450509afaf4067e140edd7eb0d0.tar.gz
trackermap-server-deb519ebd6798450509afaf4067e140edd7eb0d0.tar.bz2
trackermap-server-deb519ebd6798450509afaf4067e140edd7eb0d0.zip
Fix concurrency issues
Diffstat (limited to 'src/main/java/org/traccar/database/CommandsManager.java')
-rw-r--r--src/main/java/org/traccar/database/CommandsManager.java29
1 files changed, 24 insertions, 5 deletions
diff --git a/src/main/java/org/traccar/database/CommandsManager.java b/src/main/java/org/traccar/database/CommandsManager.java
index dc9512d9e..de6eeeba8 100644
--- a/src/main/java/org/traccar/database/CommandsManager.java
+++ b/src/main/java/org/traccar/database/CommandsManager.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 - 2020 Anton Tananaev (anton@traccar.org)
* Copyright 2017 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -142,14 +142,33 @@ public class CommandsManager extends ExtendedObjectManager<Command> {
}
private Queue<Command> getDeviceQueue(long deviceId) {
- if (!deviceQueues.containsKey(deviceId)) {
- deviceQueues.put(deviceId, new ConcurrentLinkedQueue<Command>());
+ Queue<Command> deviceQueue;
+ try {
+ readLock();
+ deviceQueue = deviceQueues.get(deviceId);
+ } finally {
+ readUnlock();
+ }
+ if (deviceQueue != null) {
+ return deviceQueue;
+ } else {
+ try {
+ writeLock();
+ return deviceQueues.computeIfAbsent(deviceId, key -> new ConcurrentLinkedQueue<>());
+ } finally {
+ writeUnlock();
+ }
}
- return deviceQueues.get(deviceId);
}
public void sendQueuedCommands(ActiveDevice activeDevice) {
- Queue<Command> deviceQueue = deviceQueues.get(activeDevice.getDeviceId());
+ Queue<Command> deviceQueue;
+ try {
+ readLock();
+ deviceQueue = deviceQueues.get(activeDevice.getDeviceId());
+ } finally {
+ readUnlock();
+ }
if (deviceQueue != null) {
Command command = deviceQueue.poll();
while (command != null) {