net.sf.robocode.host.security.RobotThreadManager Maven / Gradle / Ivy
/*
* Copyright (c) 2001-2023 Mathew A. Nelson and Robocode contributors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://robocode.sourceforge.io/license/epl-v10.html
*/
package net.sf.robocode.host.security;
import net.sf.robocode.host.IHostedThread;
import net.sf.robocode.host.IThreadManager;
import net.sf.robocode.io.Logger;
import net.sf.robocode.io.RobocodeProperties;
import static net.sf.robocode.io.Logger.logError;
import static net.sf.robocode.io.Logger.logMessage;
import static net.sf.robocode.io.Logger.logWarning;
import robocode.exception.RobotException;
import java.lang.reflect.InvocationTargetException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.Map;
/**
* @author Mathew A. Nelson (original)
* @author Flemming N. Larsen (contributor)
* @author Pavel Savara (contributor)
*/
public class RobotThreadManager {
private final IHostedThread robotProxy;
private Thread runThread;
private ThreadGroup runThreadGroup;
private Object awtForThreadGroup;
private final Map disposeAppContextThreadMap = new HashMap();
public RobotThreadManager(IHostedThread robotProxy) {
this.robotProxy = robotProxy;
createThreadGroup();
}
public void cleanup() {
try {
if (runThread == null || !runThread.isAlive()) {
if (!discardAWT()) {
runThreadGroup.destroy();
}
} else {
Logger.logWarning("Could not destroy " + runThread.getName());
}
} catch (Exception e) {
Logger.logError("Could not destroy " + runThreadGroup.getName(), e);
}
}
public void initAWT() {
if (awtForThreadGroup == null) {
awtForThreadGroup = AccessController.doPrivileged(new PrivilegedAction