title: Access variable from another class from a thread
body: <p>At the moment a key event is generated in the GUI which is then passed through several classes and given to a class which is running in a separate thread. The thread is waiting for a key event and when one is received a variable from further up the class chain is altered (see diagram). However during debug the variable is not changing.</p>
  
  <p>The class that the thread is accessing is of course in its own thread as it is being called from the GUI which has led me to think that this is a problem with concurrency.</p>
  
  <p>Is there a way to solve this perhaps using atomic integers or locks? I've seen a few examples using the synchronized functions however I can't get them to work as they don't explain the requirements of the classes. (by which I mean they give you the code to do the synchronization but they do not explain how to make a class "synchronisable").</p>
  
  <p><img src="https://i.stack.imgur.com/xyLWt.png" alt="Diagram of class structure">
  Here is the code from the Thread in class E, as you can the the thread's object reference is set from the class above which receives a reference of Class A from the class above etc.</p>
  
  <pre><code>    private Processor processor;
  
      public void run() {
          while (true) {
              if (keyevent != null) {
  
  
                  keyevent = null;
                  processor.I = 4;
              }
          }
      }
  
      public void SetProcessor(Processor processor) {
          this.processor = processor;
      }
  </code></pre>
  
  <p>An expansion on the debugging comment. During debug if I only debug the thread in class E and step through it the code functions fine and the processor.I receives the value of four. However when i am not debugging that thread nothing happens in the processor which is why I thought it might be a concurrency problem.</p>
  
  <p>Made the varaible that I am accessing in Class B and Atomic Integer, also made some of the functions used synchronized as well. Still dosent function outside of debug environment :(</p>
  
  <p>Code in Class B called from Class E</p>
  
  <pre><code>    public void SetI(int value){//also tried using synchronized as well
          I.set(value);
      }
  </code></pre>
  
  <p>The KeyEvent is generated in the GUI class by a keyListener (which fires whenver a key is pressed). The KeyEvent object is then passed to class E via several "trickle down" functions that simply pass it onto the next class, so the GUI calls processor.setKeyevent(e), the processor then call bus.setKeyevent(e) and so on a so forth until the KeyEvent property is set in Class E.</p>
  
  <p>Upon initialization of the system the thread in class E is started, is constantly checks the value of the Keyevent property, once the KeyEvent is not null i.e. it has been passed one from the GUI (via everything else) Class E then sets the value of an integer property in class B.</p>
  
  <p>What is happening is that when a key is pressed nothing happens what should be happening is that the integer is class B should be changing because of Class E however it is not. As net beans does not allow me to debug two threads at once its makes it a bit awkward, when I put breakpoints in the code outside of the thread in class E, it does not work, its as if the thread is not running or as if its not receiving the keyevent, if i put breakpoints in the thread and not outside it works, the value of I in class B is changed. If it is run outside of debug it dose not work :/</p>