Re: Thread stuck in state NEW?
Tom Hawtin wrote:
Thread. It calls run on the argument, in the Event Dispatch Thread
Ok, I think I get it. The EDT just calls run() directly, achieving
synchronization by running the code in it's own thread. Gotcha. Now I
understand why Thread was never invoked with start().
There are a number of ways to get results back from Runnable. The
obvious way is to go one level up from an anonymous inner class and use
a local class.
I'd thought of this, but I didn't have any need for a local class, and
an anonymous class seemed to fit the overall design better.
If you don't mind being a little obscure, you can do it in a terser
fashion.
return new Runnable() {
String result;
public void run() {
result = (String)JOptionPane.showInputDialog(
JOptionPane.showInputDialog(
null, "Enter a random string:"
);
}
// (this is an instance initialiser)
{
java.awt.EventQueue.invokeAndWait(this);
}
}.result;
Wooooooow, smmmooooooottttthhhhh!! I'm highly impressed. Just the sort
of trick I was looking for.
Alternatively, a more sophisticated approach is to have the main thread
waiting on a (java.util.concurrent) blocking queue. This could be
extended to make the main thread become event based (not shown).
I thought of this too, but declaring a concurrent object that going to
be used once, for one piece of data, seemed a little "heavy weight" to
me. It or the local class thing was going to be my "last ditch" effort.
Thanks for that great summary of ideas!!