Fixed thread pool leak and work queue growing.

This commit is contained in:
magenoxx 2011-06-22 20:36:48 +04:00
parent c7ae032d5a
commit cfa779661d
5 changed files with 24 additions and 4 deletions

View file

@ -32,6 +32,7 @@ import java.rmi.RemoteException;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.logging.Level; import java.util.logging.Level;
import mage.game.draft.Draft; import mage.game.draft.Draft;
@ -142,6 +143,7 @@ public class DraftSession {
private synchronized void cancelTimeout() { private synchronized void cancelTimeout() {
if (futureTimeout != null) { if (futureTimeout != null) {
futureTimeout.cancel(false); futureTimeout.cancel(false);
((ThreadPoolExecutor)timeoutExecutor).getQueue().remove(futureTimeout);
} }
} }

View file

@ -278,7 +278,8 @@ public class GameController implements GameCallback {
public void kill(UUID sessionId) { public void kill(UUID sessionId) {
if (sessionPlayerMap.containsKey(sessionId)) { if (sessionPlayerMap.containsKey(sessionId)) {
gameSessions.get(sessionPlayerMap.get(sessionId)).setKilled(); GameSession session = gameSessions.get(sessionPlayerMap.get(sessionId));
session.destroy();
gameSessions.remove(sessionPlayerMap.get(sessionId)); gameSessions.remove(sessionPlayerMap.get(sessionId));
leave(sessionId); leave(sessionId);
sessionPlayerMap.remove(sessionId); sessionPlayerMap.remove(sessionId);

View file

@ -34,6 +34,7 @@ import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import mage.game.Game; import mage.game.Game;
import mage.MageException; import mage.MageException;
@ -172,6 +173,8 @@ public class GameSession extends GameWatcher {
private synchronized void cancelTimeout() { private synchronized void cancelTimeout() {
if (futureTimeout != null) { if (futureTimeout != null) {
futureTimeout.cancel(false); futureTimeout.cancel(false);
((ThreadPoolExecutor)timeoutExecutor).getQueue().remove(futureTimeout);
//System.out.println("tasks:"+ ((ThreadPoolExecutor)timeoutExecutor).getTaskCount());
} }
} }
@ -194,4 +197,9 @@ public class GameSession extends GameWatcher {
cancelTimeout(); cancelTimeout();
game.getPlayer(playerId).setResponseInteger(data); game.getPlayer(playerId).setResponseInteger(data);
} }
public void destroy() {
cancelTimeout();
setKilled();
}
} }

View file

@ -32,6 +32,7 @@ import java.rmi.RemoteException;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.logging.Level; import java.util.logging.Level;
import mage.cards.decks.Deck; import mage.cards.decks.Deck;
@ -146,6 +147,7 @@ public class TournamentSession {
private synchronized void cancelTimeout() { private synchronized void cancelTimeout() {
if (futureTimeout != null) { if (futureTimeout != null) {
futureTimeout.cancel(false); futureTimeout.cancel(false);
((ThreadPoolExecutor)timeoutExecutor).getQueue().remove(futureTimeout);
} }
} }

View file

@ -28,9 +28,7 @@
package mage.server.util; package mage.server.util;
import java.util.concurrent.ExecutorService; import java.util.concurrent.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
/** /**
* *
@ -42,6 +40,15 @@ public class ThreadExecutor {
private static ExecutorService gameExecutor = Executors.newFixedThreadPool(ConfigSettings.getInstance().getMaxGameThreads()); private static ExecutorService gameExecutor = Executors.newFixedThreadPool(ConfigSettings.getInstance().getMaxGameThreads());
private static ScheduledExecutorService timeoutExecutor = Executors.newScheduledThreadPool(5); private static ScheduledExecutorService timeoutExecutor = Executors.newScheduledThreadPool(5);
static {
((ThreadPoolExecutor)rmiExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
((ThreadPoolExecutor)rmiExecutor).allowCoreThreadTimeOut(true);
((ThreadPoolExecutor)gameExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
((ThreadPoolExecutor)gameExecutor).allowCoreThreadTimeOut(true);
((ThreadPoolExecutor)timeoutExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
((ThreadPoolExecutor)timeoutExecutor).allowCoreThreadTimeOut(true);
}
private final static ThreadExecutor INSTANCE = new ThreadExecutor(); private final static ThreadExecutor INSTANCE = new ThreadExecutor();
public static ThreadExecutor getInstance() { public static ThreadExecutor getInstance() {