nanohttpd: refactor: AsyncRunner: use concurrent data structures

This commit is contained in:
Haowei Wen 2020-08-26 19:33:00 +08:00
parent bac4022ada
commit e56a68ecc3
No known key found for this signature in database
GPG key ID: 5BC167F73EA558E4

View file

@ -53,9 +53,9 @@ import java.io.OutputStream;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import moe.yushi.authlibinjector.internal.fi.iki.elonen.HTTPSession.ConnectionCloseException; import moe.yushi.authlibinjector.internal.fi.iki.elonen.HTTPSession.ConnectionCloseException;
@ -131,13 +131,11 @@ public abstract class NanoHTTPD {
*/ */
private static class AsyncRunner { private static class AsyncRunner {
private long requestCount; private final AtomicLong requestCount = new AtomicLong();
private final List<ClientHandler> running = new CopyOnWriteArrayList<>();
private final List<ClientHandler> running = Collections.synchronizedList(new ArrayList<NanoHTTPD.ClientHandler>());
public void closeAll() { public void closeAll() {
// copy of the list for concurrency for (ClientHandler clientHandler : this.running) {
for (ClientHandler clientHandler : new ArrayList<>(this.running)) {
clientHandler.close(); clientHandler.close();
} }
} }
@ -147,10 +145,9 @@ public abstract class NanoHTTPD {
} }
public void exec(ClientHandler clientHandler) { public void exec(ClientHandler clientHandler) {
++this.requestCount;
Thread t = new Thread(clientHandler); Thread t = new Thread(clientHandler);
t.setDaemon(true); t.setDaemon(true);
t.setName("NanoHttpd Request Processor (#" + this.requestCount + ")"); t.setName("NanoHttpd Request Processor (#" + this.requestCount.incrementAndGet() + ")");
this.running.add(clientHandler); this.running.add(clientHandler);
t.start(); t.start();
} }