Fix SafeRandom logic

This commit is contained in:
Moritz 'Morty' Strübe 2015-10-15 09:56:51 +02:00
parent 6f86e0bd92
commit f17fc20fe7

View file

@ -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) {