public void startModelValidating() {
   if (!isModelValid() && !validationStarted) {
     validationStarted = true;
     EventQueue.invokeLater(
         () -> {
           Scripts.LocalContext context = Scripts.getContext();
           RP.execute(
               () -> {
                 Scripts.setContext(context);
                 try {
                   validateModel();
                 } catch (Exception ex) {
                   Logger.getLogger(PlatypusDataObject.class.getName())
                       .log(Level.WARNING, ex.getMessage(), ex);
                 } finally {
                   Scripts.setContext(null);
                   EventQueue.invokeLater(
                       () -> {
                         validationStarted = false;
                         setModelValid(true);
                       });
                 }
               });
         });
   }
 }
 public void process(Runnable aTask) {
   Scripts.LocalContext context = Scripts.getContext();
   Runnable taskWrapper =
       () -> {
         Scripts.setContext(context);
         try {
           aTask.run();
         } finally {
           Scripts.setContext(null);
         }
       };
   queue.offer(taskWrapper);
   offerTask(
       () -> {
         // Runnable processedTask = taskWrapper;
         int version;
         int newVersion;
         Thread thisThread = Thread.currentThread();
         do {
           version = queueVersion.get();
           // Zombie counter ...
           newVersion = version + 1;
           if (newVersion == Integer.MAX_VALUE) {
             newVersion = 0;
           }
           /* moved to top of body
           if (processedTask != null) {//Single attempt to offer aTask.
           queue.offer(processedTask);
           processedTask = null;
           }
           */
           if (worker.compareAndSet(null, thisThread)) { // Worker electing.
             try {
               // already single threaded environment
               if (global == null) {
                 Scripts.Space.this.initSpaceGlobal();
               }
               // Zombie processing ...
               Runnable task = queue.poll();
               while (task != null) {
                 task.run();
                 task = queue.poll();
               }
             } catch (Throwable t) {
               Logger.getLogger(Scripts.class.getName()).log(Level.SEVERE, null, t);
             } finally {
               boolean setted = worker.compareAndSet(thisThread, null);
               assert setted : "Worker electing assumption failed"; // Always successfull CAS.
             }
           }
         } while (!queueVersion.compareAndSet(version, newVersion));
       });
 }
 public static void startBIO(Runnable aBioTask) {
   LocalContext context = getContext();
   context.incAsyncsCount();
   bio.submit(
       () -> {
         setContext(context);
         try {
           aBioTask.run();
         } finally {
           setContext(null);
         }
       });
 }
 void initSpaceGlobal() {
   Bindings bindings = engine.createBindings();
   scriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
   bindings.put("space", this);
   try {
     Scripts.LocalContext ctx = Scripts.createContext(Scripts.Space.this);
     Scripts.setContext(ctx);
     try {
       scriptContext.setAttribute(
           ScriptEngine.FILENAME, PLATYPUS_JS_FILENAME, ScriptContext.ENGINE_SCOPE);
       engine.eval(new URLReader(platypusJsUrl), scriptContext);
     } finally {
       Scripts.setContext(null);
     }
   } catch (ScriptException ex) {
     Logger.getLogger(Scripts.class.getName()).log(Level.SEVERE, null, ex);
   }
 }
 public void schedule(JSObject aJsTask, long aTimeout) {
   Scripts.LocalContext context = Scripts.getContext();
   bio.submit(
       () -> {
         try {
           Thread.sleep(aTimeout);
           Scripts.setContext(context);
           try {
             process(
                 () -> {
                   aJsTask.call(null, new Object[] {});
                 });
           } finally {
             Scripts.setContext(null);
           }
         } catch (InterruptedException ex) {
           Logger.getLogger(Scripts.class.getName()).log(Level.SEVERE, null, ex);
         }
       });
 }