Synchronization when collecting data from the EDT?
If you want to remove some data from the EDT and use it in another
thread, does EventQueue.invokeLater() or invokeAndWait() constitute
happens before? I don't think it does, so in that case, if you need to
access some data from your GUI components and use that data on another
thread there has to be some synchronization.
Object obj = new Object(); // use for lock
String str;
EventQueue.invokeLater(new Runnable() {
public void run() {
synchronized (obj) {
str = someJTextField.getText();
}
}
});
// execution should stop here until the EDT releases the lock on obj
synchronized (obj) {
// str should be visible and current here
System.out.println(str);
}
I originally thought that it would be better to use invokeAndWait() to
go get the data but after some research it appears that if the
Runnable() throws an Exception or if the current thread is interrupted,
execution of the current thread will continue. Also the Runnable() will
continue to execute to its end. So it appears that unless you need to
trap exceptions from the Runnable() in the current thread, invokeLater()
is probably a simpler solution.
What do you think about all this?
Thanks,
--
Knute Johnson
s/knute/nospam/