diff --git a/tools/cooja/java/org/contikios/cooja/SafeRandom.java b/tools/cooja/java/org/contikios/cooja/SafeRandom.java index 7c428c8cb..07c46ff1e 100644 --- a/tools/cooja/java/org/contikios/cooja/SafeRandom.java +++ b/tools/cooja/java/org/contikios/cooja/SafeRandom.java @@ -45,17 +45,30 @@ public class SafeRandom extends Random { Simulation sim = null; Thread initThread = null; + Boolean simStarted = false; private void assertSimThread() { - // It we are in the simulation thread everything is fine (the default) // sim can be null, because setSeed is called by the super-constructor. - if(sim != null && !sim.isSimulationThread()) { - // The thread initializing the simulation might differ from the simulation thread. - // If they are the same that is ok, too. + if(sim == null) return; + + // If we are in the simulation thread, everything is fine (the default) + if(sim.isSimulationThread()) { + simStarted = true; + return; + } + + // Simulation has not started yet. Allow one initialisation thread. + if(!simStarted) { if(initThread == null) initThread = Thread.currentThread(); if(Thread.currentThread() == initThread ) return; - throw new RuntimeException("A random-function was not called from the simulation thread. This can break things!"); } + + //This is done via the GUI - reproducibility is lost anyway! + if(javax.swing.SwingUtilities.isEventDispatchThread()) return; + + // Some other threads seems to access the PNRG. This must not happen. + throw new RuntimeException("A random-function was not called from the simulation thread. This can break things!"); + } public SafeRandom(Simulation sim) {