Originally posted by Peter Chase:
1) Is it allowed or advisable to create threads in the library code, to do asynchronous tasks like lazy update? Or will this break Tomcat?
Interesting questions! It's allowed; JSP doesn't really care what your back-end libraries do. Whether they're multi-threaded or not is something of an implementation detail. (For instance, I described in an article a year or so ago in Dr. Dobbs how using multiple threads with recursion is a convenient way to acquire a sensible Iterator from an algorithm that's most naturally expressed recursively. The point is, the fact that the library uses two threads is an implementation detail; nobody else needs even to know that the extra threads are present.)
Whether or not it's advisable is up to the libraries you're using. Introducing threading can complicate the library and your JSP pages. Keep in mind a few things: if your library isn't re-entrant (that is, if it can't be called multiple times before the first invocation breaks), it will probably break if called directly from a JSP page because multiple pages might be started nearly simultaneously, in response to HTTP requests. Similarly, imagine your library produces ten threads; if you get 50 requests, you suddely have 500 threads, which is somewhat burdensome even on a good JVM. On most systems, you'll run out of memory before 1000 threads
Originally posted by Peter Chase:
2) What alternative strategies exist for asynchronous tasks?
Unfortunately, I'm not sure I understand the scope of the question. In principle, though, you have two different kinds of strategies available: you can either (a) spawn threads and wait for them to return, committing your response only once your work is done; (b) spawn threads and return immediately, treating the threads as "batch processing."
Originally posted by Peter Chase:
I ask, because my libraries, not particularly coded to work with Tomcat, make use of background threads. All seems to work great on my development Web server, but some of the application's pages seem to get "wedged" and never display on the production Web server.
It's hard to debug a problem like this with only that level of detail. There could be a number of issues -- security managers on the production server, for instance. But in principle, I don't know of any reason why it shouldn't work, short of the caveats I've mentioned here.
Hope that helps in some way.