-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Carl,

Others have already responded, but I figured I'd add my two cents, too.

On 3/10/15 11:25 AM, Carl Dreher wrote:
>> If I write a servlet such as the above, is there ever only once 
>> instance of it running?
> 
>>> Don't confuse objects with threads.  There is one instance of
>>> a particular servlet, but many threads may be executing in it 
>>> concurrently, with each thread processing a separate request.
> 
> I understand that each request is handled by a separate thread.
> But does each thread have its own copy of the servlet code?

No. The code itself is ... just code. Do you mean "does each thread
have a separate instance of the [whatever]Servlet class to use"?
That's usually not the case, either.

> Or does each thread request the use of the servlet, wait until it
> is available, use it, and then release it back to be used by the
> next thread, sort of like a database connection?

There is a marker interface called SingleThreadModel that can be used
to indicate that a servlet is NOT threadsafe and therefore must only
be accessed by a single thread at a time. Most containers will
automatically create a handful of instances so that multiple threads
can use that servlet at once -- though they will be working with
different instances of the servlet.

That interface was deprecated in Servlet 2.4 with no replacement, but
since the API is supposed to be backward-compatible, the damage has
been done and therefore SingleThreadModel must be supported ... pretty
much forever.

Anyhow, if your servlet is not threadsafe, you're doing it wrong and
should fix your servlet.

Note that any local references and objects that are used /inside/ of
the service(...) method are usually thread safe. Basically, making a
servlet threadsafe only requires you to avoid any non-threadsafe class
members in the servlet itself... and to make sure any other objects
you access are thread-safe or you are sure aren't visible to other
threads (a good bet if your thread creates those objects and doesn't
leak references anywhere that is visible to other threads).

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: GPGTools - http://gpgtools.org

iQIcBAEBCAAGBQJU/y1OAAoJEBzwKT+lPKRYQH0P/08BynLMXtljQ7EFdJ7SVKvS
cUPIYGE31Nzk4f7GlV4lpWiS5U32dimMsa9IAZIbvaxz+dPTubaIgbN4QbFmwEbU
SlQ9wYjZPxM5CBRSI1x5o0wjlWoeo8Qq7WKl8JvqIMjpqaaHKFivt9SlwupuC0za
UR6h1y0xhNizwWBQaN5NfSsXy/12TUlwivSfD9jDrokKsnGpncN3v+0sZ7u2e5vF
pUKZ1Ad0OuLFC4Yjt4YI8cMjAkapqJOUIZ5UWiFiVJ1tC6YUk4H403ydgQW6pMWV
AaX+q5XicrbjeSfzYk45YZZi1DM0fItQdICv5+WBJgphr3dtu5cW9BBP/CXZWWfs
Y90JoTBNXQRLBUvXkf59nmjMcIUYrCj6fxyyGz3NmuGDzdDyGh4Ep9dQQACWsCez
8fSi9Zlxmau9KZSh51Fm/pKpiar3WxcNWIupiOYfG1rNAktbHW0RDmneJWmrx07Q
ShaSbDKrZ2VxFE0p4O97RwivgTfYYt+iEQYenqKB4wiiQ87xcKM5Z1kk/2MFqRQU
TyVa2h3laLLGdBY5Hwejb5blDHK99dfk40ZECUj4PIMJSYdcqxkAvoUBiAhVG6eD
ArRVSah4+0p9b7eAk5a12OhzQcf99GEusHcAy1AYKncJeVCpbqY131jQBG90LX3c
JyxPYKSX1o6jqe+9eYh2
=WEpx
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to