I think that you need to use the Singleton pattern with your MessageSource
class. This pattern ensures that only one instance of the class exists and
it will be shared amoungst all threads requesting access to an instance. The
pattern is quite simple, the constructor is made private and a new static
method is provided to get instances (eg getInstance()). This new static
method guarantees that only one instance exists by mainting a private static
pointer to the only instance and if it has already been instantited it
returns the existsing instance. e.g.:

class MessageSource
{
        private MessageSource instance = null;

        private MessageSource()
        {
        }

        public static MessageSource getInstance()
        {
                if (instance == null)
                        instance = new MessageSource();

                return instance;
        }
}

> -----Original Message-----
> From: Carter, Scott [SMTP:[EMAIL PROTECTED]]
> Sent: 06 December 2002 05:48
> To:   [EMAIL PROTECTED]
> Subject:      Threads, Observer, notifyObservers, and synchronized
>
> The Problem:  This problem is based on the chat room example from chapter
> 10
> of Jason Hunter's OReilly Java Servlet Programming.
>         The example consists of a servlet, a message source class for
> holding and sending messages and a message sink class that is an observer
> of
> the message source that returns the message.
>
> This is where my misunderstanding comes in:  the servlet contains the line
> "MessageSource source = new MessageSource();"  which in my understanding
> gives the servlet thread its own instance of the MessageSource object
> called
> source.  The MessageSource class extends Observable, and if a message is
> sent to MessageSource, it calls notifyObservers.
>
> When a new message comes into the servlet to the doGet method (used for
> waiting for a new message), the servlet asks for the next message from the
> message sink with the line "String returnMsg = new
> MessageSink().getNextMessage(source, room);"  .  So the servlet calls its
> own instance of MessageSink. MessageSink is an Observer of the source with
> the line "source.addObserver(this);" where the source is the source
> instance
> passed from the servlet.
>
> Now, if my understanding is correct each independent thread of the servlet
> has its own instance of the source and its own instance of the MessageSink
> that is an observer of its own source object.  I want to know how when one
> client posts a message, how do the other threads of the servlet (the other
> clients) get the message???
>
> The only other thing that I think is relevant is the methods of the
> MessageSink class are synchronized, but I still do not see how this would
> help given that there are different threads.
>
> Thanks for the help - Scott Carter
>
> __________________________________________________________________________
> _
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
> body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
--

It is the strict policy of Truworths that its e-mail facility and all
e-mail communications emanating therefrom, should be utilised for
business purposes only and should conform to high professional and
business standards.   Truworths has stipulated certain regulations in
terms whereof strict guidelines relating to the use and content of
e-mail communications are laid down. The use of the Truworths e-mail
facility is not permitted for the distribution of chain letters or
offensive mail of any nature whatsoever.   Truworths hereby distances
itself from and accepts no liability in respect of the unauthorised
use of its e-mail facility or the sending of e-mail communications
for other than strictly business purposes.   Truworths furthermore
disclaims liability for any  unauthorised instruction for  which
permission was not granted.    Truworths Limited accepts no liability
for any consequences arising from or as a result of reliance on this
message unless it is in respect of bona fide Truworths business for
which proper authorisation has been granted.

Any recipient of an unacceptable communication, a chain letter or
offensive material of any nature is requested to notify the Truworths
e-mail administrator ([EMAIL PROTECTED]) immediately in order that
appropriate action can be taken against the individual concerned.

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to