Warranty of storing variable value before next command in java -
i curious if after value assign command this:
a = 5; there warranty new value has been stored variable? , if different primitive , other data types? , runnable objects.
i have run method in myclass containing:
synchronized(this){ while(pleasewait){ try { system.out.println("sleeping"); wait();} catch (exception e) { e.printstacktrace(); } } } is other classes calling method sleepme() contains:
synchronized (myclass){ myclass.pleasewait = true; myclass.notify(); } and question is, have insert waiting after calling sleepme, instance of myclass have time change value of myclass.pleasewait ?
i have several set methods, assing complex objects (objects of objects) instance of myclass.
thanks
the fact 2 threads synchronizing on same object means changes made pleasewait 1 thread visible other thread when returns wait() call. synchronization (in case regaining of lock on this when return wait() call) provides required "happens before" relationship between 2 threads ensure update visible.
so answer question:
any solution requires 1 thread "wait" in sense. that's inherent in problem.
what doing sufficient. don't need add additional waiting. wait/notify mechanism ensures thread
wait()see up-to-date copy of variable ... provided changes made while holding object lock.you can implement kind of thing using higher level concurrency classes.
you implement using
volatile, "busy waiting" ... bad idea.
Comments
Post a Comment