title: JavaFX MultiThreading Flashing Lights
body: <p>I have two circles redCircle and greenCircle : -</p>
  
  <pre><code>Circle greenCircle = new Circle(250,150 ,100, Color.TRANSPARENT);
  Circle redCircle = new Circle(250,450,100,Color.TRANSPARENT);        
  greenCircle.setStroke(Color.GREEN);
  greenCircle.setStrokeWidth(4);
  group.getChildren().add(greenCircle); 
  redCircle.setStroke(Color.RED);
  redCircle.setStrokeWidth(4);
  group.getChildren().add(redCircle);  
  </code></pre>
  
  <p>Basically I want Circle to turn on and off twice in 2 seconds. So I am able to turn on Light then wait for 0.5 second , turn off and again wait for 0.5 second and turn on Light. I am not about to turn off after 0.5 second.</p>
  
  <pre><code> public class LightOn {
                      public Task&lt;Void&gt; runLightOn() throws InterruptedException {
                           return new Task&lt;Void&gt;(){
                               @Override
                               protected Void call() throws Exception { 
                                   greenCircle.setFill(Color.GREEN);                              
                                   return null;
                               }                            
                           };
                      }                   
                  }
                  public class LightOff {
                      public void perform() throws InterruptedException {
                          LightOn onL = new LightOn();
                          Task&lt;Void&gt; runLinghtOnTask = onL.runLightOn();
                          runLinghtOnTask.setOnSucceeded(new EventHandler&lt;WorkerStateEvent&gt;() {
                              @Override
                              public void handle(WorkerStateEvent event) {                              
                                 try {                                     
                                     Thread.sleep(500);
                                  } catch (InterruptedException ex) {
                                     Logger.getLogger(MovementEventsDemo.class.getName()).log(Level.SEVERE, null, ex);
                                  }
                                   greenCircle.setFill(Color.TRANSPARENT);                               
                                 nextFunction();
                              }
                          });
                          new Thread(runLinghtOnTask).start();
                      }                     
                  }
                  public void nextFunction(){
                              Task&lt;Void&gt; sleeper2 = new Task&lt;Void&gt;() {
                              @Override
                              protected Void call() throws Exception {
  
                                  try {                                               
                                              Thread.sleep(500);      
  
                                  } catch (InterruptedException e) {
                                  }
                                  return null;
                              }
                          };                 
                          sleeper2.setOnSucceeded(new EventHandler&lt;WorkerStateEvent&gt;() {
                           @Override
                           public void handle(WorkerStateEvent event) {
                               if(greenCircle.getFill()==Color.GREEN) {
                                   greenCircle.setFill(Color.TRANSPARENT); 
                               }else {
                                    greenCircle.setFill(Color.GREEN);                 
                               }                        
                          }
                       });                
                          new Thread(sleeper2).start();
                  }     
  </code></pre>
  
  <p>I use this to execute : -</p>
  
  <pre><code> LightOff lf = new LightOff();
    lf.perform();
  </code></pre>