Skip to content

Commit 8559a11

Browse files
committed
replaced HasMap with ConcurrentHashMap to fix issues with multithreading
1 parent 640fccb commit 8559a11

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/main/java/com/github/rosjava_actionlib/ActionServer.java

+32-7
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@
2727

2828
import java.lang.reflect.Method;
2929
import java.util.HashMap;
30+
import java.util.Map;
3031
import java.util.Timer;
3132
import java.util.TimerTask;
3233
import java.util.Vector;
3334
import java.util.concurrent.TimeUnit;
35+
import java.util.Collection;
36+
import java.util.Iterator;
37+
import java.util.concurrent.ConcurrentHashMap;
3438

3539
/**
36-
* Class to encapsulate the actiolib server's communication and goal management.
40+
* Class to encapsulate the actionlib server's communication and goal management.
3741
*
3842
* @author Ernesto Corbellini [email protected]
3943
*/
@@ -64,8 +68,9 @@ private class ServerGoal {
6468
private String actionName;
6569
private ActionServerListener callbackTarget = null;
6670
private Timer statusTick = new Timer();
67-
private HashMap<String, ServerGoal> goalTracker = new HashMap<String,
68-
ServerGoal>(1);
71+
private ConcurrentHashMap<String,ServerGoal> goalTracker = new ConcurrentHashMap<String,ServerGoal>(1);
72+
//private HashMap<String, ServerGoal> goalTracker = new HashMap<String,
73+
// ServerGoal>(1);
6974

7075
/**
7176
* Constructor.
@@ -151,9 +156,9 @@ public void run() {
151156
* Stop publishing the action server topics.
152157
*/
153158
private void unpublishServer() {
154-
// Stop the task run by the Timer.
155-
statusTick.cancel();
156-
statusTick.purge();
159+
// stop the task run by the Timer.
160+
statusTick.cancel();
161+
statusTick.purge();
157162

158163
if (statusPublisher != null) {
159164
statusPublisher.shutdown(5, TimeUnit.SECONDS);
@@ -259,12 +264,32 @@ public void sendStatusTick() {
259264
GoalStatus goalStatus;
260265
Vector<GoalStatus> goalStatusList = new Vector<GoalStatus>();
261266

262-
for (ServerGoal sg : goalTracker.values()) {
267+
Iterator it = goalTracker.entrySet().iterator();
268+
while (it.hasNext()) {
269+
Map.Entry pair = (Map.Entry) it.next();
270+
ServerGoal sg = (ServerGoal) pair.getValue();
263271
goalStatus = node.getTopicMessageFactory().newFromType(GoalStatus._TYPE);
264272
goalStatus.setGoalId(getGoalId(sg.goal));
265273
goalStatus.setStatus((byte) sg.state.getState());
266274
goalStatusList.add(goalStatus);
267275
}
276+
277+
/* Collection<ServerGoal> serverGoals = goalTracker.values();
278+
279+
for (Iterator<ServerGoal> it = serverGoals.iterator(); it.hasNext();) {
280+
ServerGoal sg = it.next();
281+
goalStatus = node.getTopicMessageFactory().newFromType(GoalStatus._TYPE);
282+
goalStatus.setGoalId(getGoalId(sg.goal));
283+
goalStatus.setStatus((byte) sg.state.getState());
284+
goalStatusList.add(goalStatus);
285+
} */
286+
287+
/* for (ServerGoal sg : goalTracker.values()) {
288+
goalStatus = node.getTopicMessageFactory().newFromType(GoalStatus._TYPE);
289+
goalStatus.setGoalId(getGoalId(sg.goal));
290+
goalStatus.setStatus((byte) sg.state.getState());
291+
goalStatusList.add(goalStatus);
292+
} */
268293
status.setStatusList(goalStatusList);
269294
sendStatus(status);
270295
}

0 commit comments

Comments
 (0)