java - Access variable from another class from a thread -


at moment key event generated in gui passed through several classes , given class running in separate thread. thread waiting key event , when 1 received variable further class chain altered (see diagram). during debug variable not changing.

the class thread accessing of course in own thread being called gui has led me think problem concurrency.

is there way solve perhaps using atomic integers or locks? i've seen few examples using synchronized functions can't them work don't explain requirements of classes. (by mean give code synchronization not explain how make class "synchronisable").

diagram of class structure here code thread in class e, can the thread's object reference set class above receives reference of class class above etc.

    private processor processor;      public void run() {         while (true) {             if (keyevent != null) {                   keyevent = null;                 processor.i = 4;             }         }     }      public void setprocessor(processor processor) {         this.processor = processor;     } 

an expansion on debugging comment. during debug if debug thread in class e , step through code functions fine , processor.i receives value of four. when not debugging thread nothing happens in processor why thought might concurrency problem.

made varaible accessing in class b , atomic integer, made of functions used synchronized well. still dosent function outside of debug environment :(

code in class b called class e

    public void seti(int value){//also tried using synchronized         i.set(value);     } 

the keyevent generated in gui class keylistener (which fires whenver key pressed). keyevent object passed class e via several "trickle down" functions pass onto next class, gui calls processor.setkeyevent(e), processor call bus.setkeyevent(e) , on forth until keyevent property set in class e.

upon initialization of system thread in class e started, checks value of keyevent property, once keyevent not null i.e. has been passed 1 gui (via else) class e sets value of integer property in class b.

what happening when key pressed nothing happens should happening integer class b should changing because of class e not. net beans not allow me debug 2 threads @ once makes bit awkward, when put breakpoints in code outside of thread in class e, not work, if thread not running or if not receiving keyevent, if put breakpoints in thread , not outside works, value of in class b changed. if run outside of debug dose not work :/

class e should not directly manipulating data members in class b. kinds of bad. isn't point of problem.

in order gui thread in b see changes made thread in e, need use kind of synchronization control. suggest using atomicinteger, mentioned swing stuff, i'm assuming class b swing component. in case, find cleaner keep swing stuff on edt , make e's responsibility call b on edt.

here's mean. i've eliminated class c , d since pass-through anyway. i'm ignoring construction/setup , starting of thread. i've removed busy-loop in e , replaced countdownlatch.

/**  * gui class, should accessed edt  */ public class b extends jpanel {      private int value = 0;      private e thatthreadobject;      public void sete( e e ) {         thatthreadobject = e;     }         public void setvalue( int newvalue ) {         value = newvalue;         system.out.println( "got new int value: " + value );     }      public void triggerkeyevent() {         thatthreadobject.keyevent();     } }  /**  * must thread-safe, accessed multiple threads  */ public class e implements runnable{     private b thatguiobject;     // note, latch one-time use.     private final countdownlatch latch = new countdownlatch( 1 );      public void setb( b b ) {         thatguiobject = b;     }      public void keyevent() {         // wake waiting thread         latch.countdown();                 }      @override     public void run() {         try {             // wait key event forever, better busy looping             latch.await();             // update b, it's swing component use edt             eventqueue.invokelater( new runnable() {                 @override                 public void run() {                     thatguiobject.setvalue( 4 );                 }             } );         }         catch ( interruptedexception e ) {             e.printstacktrace();         }                 } } 

have @ java concurrency tutorial


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -