[Minor] Replace synchronizedMap() with ConcurrentHashMap

This commit is contained in:
Robert von Burg 2023-04-05 20:33:11 +02:00
parent e77e730c7c
commit 5ce2382834
Signed by: eitch
GPG Key ID: 75DB9C85C74331F7
5 changed files with 18 additions and 18 deletions

View File

@ -7,6 +7,7 @@ import static li.strolch.runtime.StrolchConstants.SYSTEM_USER_AGENT;
import static li.strolch.utils.collections.SynchronizedCollections.synchronizedMapOfMaps;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
import li.strolch.agent.api.ComponentContainer;
@ -610,7 +611,7 @@ public class EventBasedExecutionHandler extends ExecutionHandler {
}
private void evaluateStateByRealm() throws Exception {
this.statesByRealm = Collections.synchronizedMap(new HashMap<>());
this.statesByRealm = new ConcurrentHashMap<>();
runAsAgent(ctx -> getContainer().getRealmNames().forEach(realm -> {
try (StrolchTransaction tx = openTx(realm, ctx.getCertificate(), false)) {

View File

@ -3,9 +3,8 @@ package li.strolch.execution;
import static li.strolch.utils.helper.StringHelper.formatMillisecondsDuration;
import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
@ -27,7 +26,7 @@ public class SimpleDurationExecutionTimer implements DelayedExecutionTimer {
public SimpleDurationExecutionTimer(StrolchAgent agent) {
this.agent = agent;
this.simulationTasks = Collections.synchronizedMap(new HashMap<>());
this.simulationTasks = new ConcurrentHashMap<>();
}
@Override

View File

@ -15,9 +15,8 @@
*/
package li.strolch.service.executor;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import li.strolch.agent.api.ComponentContainer;
import li.strolch.agent.api.StrolchComponent;
@ -46,7 +45,7 @@ public class ServiceExecutionHandler extends StrolchComponent {
@Override
public void initialize(ComponentConfiguration configuration) throws Exception {
this.serviceContextMap = Collections.synchronizedMap(new HashMap<>());
this.serviceContextMap = new ConcurrentHashMap<>();
super.initialize(configuration);
}

View File

@ -2,10 +2,9 @@ package li.strolch.utils;
import static java.util.concurrent.Executors.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@ -22,8 +21,8 @@ public class ExecutorPool {
private final Map<String, ScheduledExecutorService> scheduledExecutors;
public ExecutorPool() {
this.executors = Collections.synchronizedMap(new HashMap<>());
this.scheduledExecutors = Collections.synchronizedMap(new HashMap<>());
this.executors = new ConcurrentHashMap<>();
this.scheduledExecutors = new ConcurrentHashMap<>();
}
public ExecutorService getExecutor(String poolName) {

View File

@ -22,6 +22,7 @@ import java.text.MessageFormat;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
@ -93,13 +94,16 @@ public class DefaultStrolchSessionHandler extends StrolchComponent implements St
@Override
public void start() throws Exception {
this.privilegeHandler = getContainer().getComponent(PrivilegeHandler.class);
this.certificateMap = Collections.synchronizedMap(new HashMap<>());
this.certificateMap = new ConcurrentHashMap<>();
if (this.reloadSessions) {
List<Certificate> certificates = runAsAgentWithResult(ctx -> {
Certificate cert = ctx.getCertificate();
return this.privilegeHandler.getPrivilegeHandler().getCertificates(cert).stream()
.filter(c -> !c.getUserState().isSystem()).collect(Collectors.toList());
return this.privilegeHandler.getPrivilegeHandler()
.getCertificates(cert)
.stream()
.filter(c -> !c.getUserState().isSystem())
.collect(Collectors.toList());
});
for (Certificate certificate : certificates) {
this.certificateMap.put(certificate.getAuthToken(), certificate);
@ -271,8 +275,7 @@ public class DefaultStrolchSessionHandler extends StrolchComponent implements St
Certificate removedCert = this.certificateMap.remove(certificate.getAuthToken());
if (removedCert == null)
logger.error(MessageFormat.format("No session was registered with token {0}",
certificate.getAuthToken()));
logger.error(MessageFormat.format("No session was registered with token {0}", certificate.getAuthToken()));
this.privilegeHandler.invalidate(certificate);
}
@ -347,8 +350,7 @@ public class DefaultStrolchSessionHandler extends StrolchComponent implements St
Certificate removedCert = this.certificateMap.remove(certificate.getAuthToken());
if (removedCert == null)
logger.error(MessageFormat.format("No session was registered with token {0}",
certificate.getAuthToken()));
logger.error(MessageFormat.format("No session was registered with token {0}", certificate.getAuthToken()));
this.privilegeHandler.sessionTimeout(certificate);
}