diff src/goodjava/webserver/handlers/DomainHandler.java @ 1601:de5a81327d9c

fix deadlock
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 09 Apr 2021 22:39:03 -0600
parents 1b809d2fdf03
children 8df0b80e715e
line wrap: on
line diff
--- a/src/goodjava/webserver/handlers/DomainHandler.java	Fri Apr 09 19:44:14 2021 -0600
+++ b/src/goodjava/webserver/handlers/DomainHandler.java	Fri Apr 09 22:39:03 2021 -0600
@@ -3,8 +3,8 @@
 import java.io.Closeable;
 import java.io.IOException;
 import java.util.Map;
-import java.util.HashMap;
 import java.util.Set;
+import java.util.Collections;
 import java.util.Timer;
 import java.util.TimerTask;
 import java.util.concurrent.ConcurrentHashMap;
@@ -37,7 +37,7 @@
 	}
 
 	private static final long HOUR = 1000L*60*60;
-	private final Map<String,Handler> map = new SoftCacheMap<String,Handler>();
+	private final Map<String,Handler> map = Collections.synchronizedMap(new SoftCacheMap<String,Handler>());
 	private final Set<Handler> dontGc = ConcurrentHashMap.newKeySet();
 	private final Timer timer = new Timer();
 
@@ -66,8 +66,8 @@
 	}
 
 	public Handler getHandler(String domain) {
-		domain = domain.toLowerCase();
-		synchronized(map) {
+		domain = domain.toLowerCase().intern();
+		synchronized(domain) {
 			Handler handler = map.get(domain);
 			if( handler == null ) {
 				//if(ref!=null) logger.info("gc "+domain);
@@ -82,8 +82,8 @@
 
 	public void removeHandler(String domain) {
 		//logger.info("removeHandler "+domain);
-		domain = domain.toLowerCase();
-		synchronized(map) {
+		domain = domain.toLowerCase().intern();
+		synchronized(domain) {
 			Handler handler = map.remove(domain);
 			if( handler != null ) {
 				close(handler);