lower simulation thread priority, added timeout on blocking stop simulation method

This commit is contained in:
fros4943 2010-03-26 09:26:22 +00:00
parent e4c3ac8125
commit 96979b5fbf

View file

@ -24,7 +24,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* $Id: Simulation.java,v 1.63 2010/02/23 21:55:55 fros4943 Exp $ * $Id: Simulation.java,v 1.64 2010/03/26 09:26:22 fros4943 Exp $
*/ */
package se.sics.cooja; package se.sics.cooja;
@ -310,14 +310,15 @@ public class Simulation extends Observable implements Runnable {
if (!isRunning()) { if (!isRunning()) {
isRunning = true; isRunning = true;
simulationThread = new Thread(this); simulationThread = new Thread(this);
simulationThread.setPriority(Thread.MIN_PRIORITY);
simulationThread.start(); simulationThread.start();
} }
} }
/** /**
* Stops simulation and conditionally blocks until stopped. * Stop simulation
* *
* @param block Blocks if true * @param block Block until simulation has stopped, with timeout (100ms)
* *
* @see #stopSimulation() * @see #stopSimulation()
*/ */
@ -325,32 +326,32 @@ public class Simulation extends Observable implements Runnable {
if (!isRunning()) { if (!isRunning()) {
return; return;
} }
if (block) {
stopSimulation();
} else {
stopSimulation = true; stopSimulation = true;
}
}
/** if (block) {
* Stops this simulation (notifies observers). if (Thread.currentThread() == simulationThread) {
* Method blocks until simulation has stopped. return;
*/ }
public void stopSimulation() {
if (isRunning()) {
stopSimulation = true;
/* Wait until simulation stops */ /* Wait until simulation stops */
if (Thread.currentThread() != simulationThread) {
try { try {
Thread simThread = simulationThread; Thread simThread = simulationThread;
if (simThread != null) { if (simThread != null) {
simThread.join(); simThread.join(100);
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
} }
} }
} }
/**
* Stop simulation (blocks).
* Calls stopSimulation(true).
*
* @see #stopSimulation(boolean)
*/
public void stopSimulation() {
stopSimulation(true);
} }
/** /**