> [JList contents occasionally display as empty]
>
[quoted text clipped - 25 lines]
>
> xanthian.
I have heard that Swing isnt thread-safe, but I assumed that this meant that
calls to any operation across multiple threads should be synchronized, but
it goes further than this it seems. I have had a look at the documentation
on the Sunwebsite about this and what came to mind was - why? Why have they
made it this way? This is a similar problem in win32 programming with
message loops across different threads - I thought I had gotten away from
this!
Anyway, the documentation is not very clear - if I want to update my list by
adding a String to it, how can I pass the String parameter? If this has to
be done with a Runnable() object to I have to pass it as a param to the
constructor?
Thanks
Allan
Thomas Hawtin - 19 Jan 2006 23:35 GMT
> I have heard that Swing isnt thread-safe, but I assumed that this meant that
Not only is Swing not thread-safe, it's not even thread-agnostic. It is,
to a certain extent, thread-hostile.
> calls to any operation across multiple threads should be synchronized, but
> it goes further than this it seems. I have had a look at the documentation
> on the Sunwebsite about this and what came to mind was - why? Why have they
> made it this way? This is a similar problem in win32 programming with
> message loops across different threads - I thought I had gotten away from
> this!
Unfortunately multithreading is not easy.
> Anyway, the documentation is not very clear - if I want to update my list by
> adding a String to it, how can I pass the String parameter? If this has to
> be done with a Runnable() object to I have to pass it as a param to the
> constructor?
The easiest way is to make the String variable final and the subtype
Runnable an anonymous inner class. Runnable is always subtyped, as it's
an interface (and has no constructors).
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Allan Bruce - 20 Jan 2006 01:37 GMT
> The easiest way is to make the String variable final and the subtype
> Runnable an anonymous inner class. Runnable is always subtyped, as it's an
> interface (and has no constructors).
>
> Tom Hawtin
It cannot be final as the contents of all of the Strings are unknown at
compile-time :(
Allan
Thomas Hawtin - 20 Jan 2006 01:59 GMT
>> The easiest way is to make the String variable final and the subtype
>> Runnable an anonymous inner class. Runnable is always subtyped, as it's an
[quoted text clipped - 4 lines]
> It cannot be final as the contents of all of the Strings are unknown at
> compile-time :(
final (as applied to a local variable) means that it can only be
assigned once. As a local variable, that will have to be during that run
of the method.
public void addUser(User user) {
assert !java.awt.EventQueue.isDispatchThread();
final String name = user.getName();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
assert java.awt.EventQueue.isDispatchThread();
list.addElement(name);
}
});
}
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Allan Bruce - 20 Jan 2006 02:27 GMT
>> It cannot be final as the contents of all of the Strings are unknown at
>> compile-time :(
[quoted text clipped - 15 lines]
>
> Tom Hawtin
Doh, of course, I feel a little stupid now! Anyway, so the above code could
be executed from any thread, other than the EDT?
Thanks
Allan
Kent Paul Dolan - 20 Jan 2006 04:54 GMT
> I have heard that Swing isnt thread-safe, but I
> assumed that this meant that calls to any
> operation across multiple threads should be
> synchronized, but it goes further than this it
> seems.
Yes. Because calls that update the graphical image
have part of their execution deferred to the Event
Handling Thread, while your computations and
alterations on the thread where you do your work
proceed apace, you are quite capable of mangling
the dataset the repainting process intends to
render to the screen, before that rendering
process begins, or, most dangerously, while it
is in progress, so that unrolling the method
calling pushdowns on the EHT stack might
bring back a list walk doing "nextItem"
using a list item which no longer exists to
point at anything.
> I have had a look at the documentation on
> the Sunwebsite about this and what came to mind
> was - why? Why have they made it this way?
Because screen painting is extremely labor
intensive; by doing lazy refreshing, they can
sometimes junk an entire repaint requirement
that has been overtaken by events by yet
another repaint request for the same components,
that supersedes the previous one.
This is called "doing dangerous optimizations", and
it will continue to bite computer programmers until
the "never" day when computer speeds become
infinite.
FWIW
xanthian.