title: How to access variables in a runnable object in Java
body: <p>I am going to simulate a traffic light system.
  I created the Road Class which extends JFrame and implements Runnable.</p>
  
  <p>Inside the run() method I added the logic to increase the Y Position of each car and It is now simulating the movements of cars.
  But now I need to check the status of the Traffic Light, before move a car.</p>
  
  <p>This is my TrafficLight class,</p>
  
  <pre><code>import java.util.Random;
  
  public class TrafficLight implements Runnable {
  
  volatile boolean stop;
  
  public TrafficLight(boolean stop) {
      this.stop = stop;
  }
  
  @Override
  public void run() {
      Random randomGenerator = new Random();
      while (true) {
          if (stop) {
              stop = false; //change current status
          } else {
              stop = true;  //change current status
          }
          try {
              Thread.sleep(2000 + randomGenerator.nextInt(2000));
          } catch (Exception ex) {
              System.out.println("error");
          }
         }
     }
  }
  </code></pre>
  
  <p>Is there any way to check this volatile variable <strong>stop</strong>, from my Road Class.</p>
  
  <p>If not please suggest me another solution to do this.</p>
  
  <p>Thanks.</p>