> -----Ursprungliche Nachricht-----
> Von: A mailing list for discussion about Sun Microsystem's Java Servlet
> API Technology. [mailto:[EMAIL PROTECTED]]Im Auftrag von
> Jeetandra Mahtani
> Gesendet: Tuesday, July 06, 1999 2:53 PM
> An: [EMAIL PROTECTED]
> Betreff: Can some explain/confirm
>
>
> Hello,
> Can someone please confirm the following and maybe provide some
> insight to make my
> concepts thorough.
> If there are no instance variables and only local variables, one
> does not need to worry
> about different threads accessing the same variable, right?
> If only local variables are being used in all classes, does one
> need to worry about
> synchronization/thread issues?
> When does one need to use the SingleThread Model ?
> Thanks for any information.
> J
>
 Hi Jeetandra,

    you are correct when you say that local variables are thread safe.
Synchronisation/Thread issues crop up when you need to share scarce
resources like Database connections between threads. For instance
I have written a (fairly basic) ConnectionPool object that pools
JDBC Connection objects. If there are no connections free in the pool
you have to wait() the current thread, so that other threads on the object
which do have a Connection can be processed. The synchronisation issues
here are in wait() ing the current thread and in notify() ing other threads
thus....

private void block() {
        suspended = true;
        connect = connPool.getConnection();
}

need not be synchronised because it is called like this...

try {
    synchronized(this) {
        while(suspended) {
            wait(); // Object is thread owner due to synchronised block
        }
        block();
     }
} catch(InterruptedException ignored) {}

the corresponding release method does have to be synchronised however

private synchronized void release() {
     suspended = false;
     // return connection back to pool here
     notify();
}

and is called in a finally block in my servlet

Note I do not use suspend() and resume() here. These
methods are deprecated as of JDK 1.1 (I think, certainly in Java 2)

the variable suspended is not local to any method and access to it is
synchronised as well as is access to the connection pool object.

SingleThread model is there when you find yourself wanting/needing to
synchronize
the service methods of your servlet for some reason. This should never
be done as it can kill off your servlet engine and the web server.
SingleThread model is resource intensive and you should try and avoid using
it at all costs (it is also slower and loads your server because each
request is
serviced by a new instance of your servlet rather than one object handling
all requests).

I myself have not needed to implement SingleThread at all nor have I even
come
close to thinking I need to. I am sure there are reasons when you have no
other choice
I am lucky enough not to ever have had to.

Hope this helps you

Andy Bailey

PS The above code is not guaranteed to work as I hacked it into this mail
without
reference to my code.

I thoroughly recommend you purchase or borrow the Java Servlet
Programming book by Jason Hunter and William Crawford and for Java generally
(especially because of threads etc) Thinking in Java by Bruce Eckel.

___________________________________________________________________________
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