Hi Stackoverflow Community,
I have a Question about Thread-safety. If I has a static Map and fill it out with different Object, are these Object Thread-safe, if i has only method they don't write in it?
I create a small Example: Is the return value of getCommand thread safe in this case?
How can I test Thread-safety with JUnit?
Controller
public class CommandController {
private static Map<String, Command> commandMap = initMap();
public static Map<String, Command> initMap() {
return new HashMap<String, Command>() {{
put("A", new CommandA());
put("B", new CommandB());
}};
}
public Command getCommand(String key) {
if(commandMap.containsKey(key)) {
return commandMap.get(key);
}
return null;
}
}
Abstract Class
public abstract class Command {
public abstract int calc(int value);
}
Command A
public class CommandB extends Command {
@Override
public int calc(int value) {
value = value * 4;
return value;
}
}
Command B
public class CommandA extends Command {
private int ab = 5;
@Override
public int calc(int value) {
return value * ab;
}
}