K
Knute Johnson
Does anybody know if the Observable/Observer calls to update() are on
the EDT?
Thanks,
the EDT?
Thanks,
Knute said:Does anybody know if the Observable/Observer calls to update() are on
the EDT?
Considering that Observer is just an interface and it would depend on
the implementation, and that no Java Swing classes that I know of
actually implement Observer, I'd have to guess the answer is in general
"no."
Knute said:The Observable class method, notifyObservers() is called and that then
calls all Observer.update() methods. I'm curious to know if the
Observable puts those calls onto the EDT.
Does anybody know if the Observable/Observer calls to update() are on
the EDT?
Thanks,
Knute Johnson said:The Observable class method, notifyObservers() is called and that
then calls all Observer.update() methods. I'm curious to know if the
Observable puts those calls onto the EDT.
As markspace has elaborated, the answer is no. Not long ago, I proposed
that on Observable can invoke notifyObservers() on the EDT, as via
javax.swing.Timer, but access to any data shared between threads must
still be correctly synchronized. I found this discussion very helpful
<http://groups.google.com/group/comp.lang.java.programmer/browse_frm/thread/4c4a6b46ffd2d31f>
and I have tried to express my understanding in this example:
<http://sites.google.com/site/drjohnbmatthews/threadwatch>
Knute Johnson said:I looked through the source code and all of the methods of Observable
are synchronized on the Observable instance. So threads that read
any argument that is passed in and stored needs to be synchronized as
well. I'm not sure how to do that. The whole point of using
Observer/Observable is to disconnect the two classes.
Calling notifyObservers() from a known thread could be used to
synchronize via thread containment but the docs imply that the
calling thread isn't necessarily guaranteed to be the writing thread
in the future.
In the application that I am writing, I am passing an Integer). In
the Observer.update() method I store that Integer. I'm storing it in
a volatile int so that solves the visibility problems for the reading
thread but what if you had a mutable Object to pass?
If the mutable Object was thread-safe, then it would handle that.
Otherwise, you'd have to use some mechanism for ensuring
synchronization. As far as I know, Java doesn't have an explicit memory
barrier API, so "synchronized" is probably the best approach for simple
mutation observability issues.
Pete
Knute Johnson said:I looked through the source code and all of the methods of Observable
are synchronized on the Observable instance. So threads that read
any argument that is passed in and stored needs to be synchronized as
well. I'm not sure how to do that. The whole point of using
Observer/Observable is to disconnect the two classes.
Interesting. The synchronized block in notifyObservers() mentions two,
worst-case potential races: 1) a newly-added Observer will miss a
notification in progress, and 2) a recently unregistered Observer will
be wrongly notified when it doesn't care. Unfortunately, the API doesn't
mention being synchronized.
Calling notifyObservers() from a known thread could be used to
synchronize via thread containment but the docs imply that the
calling thread isn't necessarily guaranteed to be the writing thread
in the future.
I read the Observable API as "but subclasses [of Observable] may ...
deliver notifications on separate threads."
In the application that I am writing, I am passing an Integer). In
the Observer.update() method I store that Integer. I'm storing it in
a volatile int so that solves the visibility problems for the reading
thread but what if you had a mutable Object to pass?
I'm not sure about that.
Knute said:And there's the rub. If you have as in my case, two separate classes,
where you don't want to share data other than that which is shared via
the Observable/Observer, how do you get the Observable instance in the
Observer to then use to synchronize with? You have to assume that a
different thread could read the data after it was stored via the
Observer.update() method.
What are you trying to do? Are you trying to pass data between threads,
or are you actually trying to implement an Observer Pattern?
If you're implementing an Observer, you should probably take care to
synchronize yourself, since the Observer really doesn't guarantee
synchronization.
If you're just looking for a memory barrier API, I think several classes
in java.util.concurrent guarantee synchronization explicitly. This
includes lock objects, executors, concurrent collections and probably a
few others. This might be as simple as dumping the data into a
BlockingQueue, and then reading it with a different thread.
Knute said:[...]
I have two classes that happen to be extensions of JPanels. Both of
them have methods to input data that is then displayed (in various
forms) on the other. The Observable/Observer appeared to be perfectly
suited for this purpose. Then I got to thinking about which thread was
being used and the fact that different threads could be
writing/reading the data. [...]
Why is that? Where is the data coming from? Both JPanels should be
executing on the EDT, assuming you haven't explicitly introduced a new
thread to them (and IMHO that would not generally be a good idea…GUI
logic belongs in the EDT, and non-GUI logic doesn't belong in a GUI
object).
Pete
Without a code example, it's hard for me to know for sure what you're
talking about. However, if you are asking how to get the reference to
the Observable instance so that you can use that reference in a
"synchronized" statement, then the answer is "you don't, nor should you".
Unless you specifically want to write some special thread-safe
Observable sub-class that handles this, the synchronization isn't
something the Observable needs to know about at all, nor would it be
anything that needs the Observable instance itself. The synchronization
is between the threads modifying the data and reading the data, while
the Observable is just a by-stander.
If you can provide a SSCCE that illustrates the relationship between
your Observable, an Observer, and whatever other code exits that is
modifying a mutable class instance, as well as examining that mutable
class instance in a different thread, then surely a solution can be
described that provides the necessary synchronization, but without
necessarily making the Observable a central figure in that.
Note that, depending on how strict you want to be about this whole
"don't want to share data other than that which is shared via the
Observable/Observer", you may require a wrapper class to impose the
synchronization needed.
But with an Observable that is not itself imposing a multi-threaded
implementation, it's not at all clear how you managed to get two classes
that know nothing of each other except for the Observable, and yet which
are going to access the same data on two different threads. Either each
class is associated with its own thread, and one knows about the other,
or only one class includes code executing on two different threads (e.g.
an Observer in one thread, and some other stuff in another), in which
case it only needs to synchronize within itself.
Basically, until it's clear how it is you've got an Observable that
involves itself in cross-thread communication, in which two different
threads involve two completely mutually-isolated classes except for an
Observable mediating, and yet in which the Observable itself isn't
involved in the threading, it's very hard to suggest a design that will
work for you.
After all, since the default Observable implementation simply calls
update() in the same thread where notifyObservers() was called, how is
it that the writer thread is different from the reader thread in the
first place?
I suspect that once you've focused on the answer to that question,
you'll see the division between the two threads where the
synchronization has to take place, and it probably will be simple to
implement that synchronization independently of the Observable behavior.
If you post a SSCCE, I'm sure you'll get good advice, if not from me
then from someone else.
Pete
Knute said:I have two classes that happen to be extensions of JPanels. Both of
them have methods to input data that is then displayed (in various
forms) on the other. The Observable/Observer appeared to be perfectly
suited for this purpose. Then I got to thinking about which thread was
being used and the fact that different threads could be writing/reading
the data.
Knute said:Panel1 p1 = new Panel1();
Panel2 p2 = new Panel2();
p1.addObserver(p2);
I don't know if you had time to read my earlier reply, but after I
posted it I starting thinking about the little design I posted. I'm
thinking that what I said--that it is probably good design to allow a
thread safe method of the observer--is just wrong.
There's two competing principles in design: 1. usefulness or ease of
use, and 2. pithiness. The opposite of pithiness is gold plating, a
known anti-pattern.
If you over emphasize 1, then you risk a baroque, ornamented design that
takes 20 method calls to accomplish what should take just one method call.
If you over emphasize 2, then you risk a stark design that only works
for your particular need, but can't be extended and is difficult to
maintain due to it being too embedded in the particular code you wrote.
Always there's a balance to maintain.
Anyway, what I'm trying to say here is that because you have a JPanel to
begin with, you have events on the EDT anyway, so why not just skip the
thread safety and say all methods have to be called on the EDT?
For example:
This is EXACTLY the example I was going to provide, implemented on the
EDT just as you have. And it doesn't need to be specially thread safe,
it's naturally called on the EDT anyway.
Yes, making the classes not thread safe risks making the class hard to
use. But most Swing classes aren't thread safe, and folks are used to
dealing with that. So there's a well developed body of knowledge for
working with Swing classes that aren't thread safe, and I don't think
you're risking much by requiring the user be responsible for thread safety.
By contrast, especially after looking at your code, I think you are
risking a baroque, gold-plated, hard-to-maintain design by trying to
make the class totally thread safe. Events fire on the EDT because it's
fast to do so; trying to spawn a separate thread just to re-issue an
event to update your 2nd panel on the EDT again seems like mis-design.
(What one of my professors used to call "going around your elbow to get
from your forefinger to your thumb.")
Anyway, here's my design. I nixed your actual panel because it was just
easier to use a JTextArea for the example, but the whole thing fires on
the EDT, so it doesn't matter. It's all thread safe, and I don't have to
make any threads, and therefore things should happen quickly and the UI
should stay fast too. The code is shorter, and runs faster. Win-win.
(Note the JSpinner doesn't actually say that a ChangeListener is
guaranteed to be called on the EDT. I assume that it is. Therefore any
observer added can assume that it's update() method is also called on
the EDT, because of my design.)
<http://sites.google.com/site/drjohnbmatthews/threadwatch> [...]
In the application that I am writing, I am passing an Integer).
In the Observer.update() method I store that Integer. I'm storing
it in a volatile int so that solves the visibility problems for
the reading thread but what if you had a mutable Object to pass?
I'm not sure about that.
Knute said:If I could be guaranteed that Observable would always call
notifyObservers() from the EDT and that I only wanted to use the data
from the EDT then there is no problem again. In my application I needed
to use the message data on the EDT and on another thread so I required
some sort of synchronization to guarantee that it would work.
If you are interested in the actual application, I have it running at:
http://rabbitbrush.frazmtn.com/aviation
Scroll to the bottom of the page and look at the VOR Trainer applet. The
source code is there as well.
So I still have one major question, how could one extend Observable and
modify notifyObservers() without access to the list of Observers? For
the example I had to re-create both Observable and Observer. Why would
Sun not have included a method to get the list of Observers?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.