Hello, Adam!
You wrote:
>
> "David" <> wrote in message
> news
ldomain...
> > Hi everyone,
> >
> > I have already started programming an application with the
following
> model:
> >
> > I have a class A and a class B.
> > Where,
> > class B extends Thread, receives data from an external source
at
> > undetermined intervals of time and interpretes this data.
The first issue is that you should be implementing Runnable not
extending Thread. It is rarely ever appropriate to extend Thread.
> > Class A is a Swing GUI which has an object of class B. Class
A has
> to get
> > data from class B in order to show it in the graphical
objects. In
> fact,
> > I'm wondering how class A would manage to know when class B
has data
> > ready? I was thinking about a tight loop in class B that
polls the
> class
> > A object to know when data from the latter is available.
I think you got your A and B backwards there, but a polling loop
is a bad idea.
> >
> > Is there some mechanism more appropriate? Any idea, url or
> reference
> > material will really help.
>
> Observer/Observable or Listener design pattern.
The event listener pattern is more appropriate here. The
Observer/Observable implementations in java.util should be
avoided.
> Make thread in B notify object of A about receiving data.
>
> Object of A could register in object B before B starts its
thread,
> and in that thread all registered listeners (in this case
object of A
> only)
> would be notified about certain state in which B decided to
> send the notification (in this case: when all data arrived.
But it is important to realize that the notification is going to
occur on the thread executing in B which can have many
ramifications. The OP said that class A which is the listener is
a Swing GUI. Swing is in general not thread safe in order to make
it faster. It is not safe to call most of the methods in Swing on
your own thread. Therefore those listeners should not invoke
Swing operations. and most likely should use invokeLater to
execute approriate code in the GUI thread where it is safe. This
however should be an implementation detail within A and B should
not care.
For more info on Swing and threads see the articles at
http://java.sun.com/products/jfc/tsc/articles
--
Dale King
My Blog:
http://daleking.homedns.org/Blog