General J2EE: Servlets - Multithreading

  1. Servlets - Multithreading (1 messages)

    Pls. help me.

    There is a plan to convert an application which uses Single Thread interface to Multithread interface to improve performance.

    The following were identified as potential changes...
    1. Static variables in servlet to be declared as final
    2. Instance variables should be moved to method variables
    3. For variables that need to be used across methods... session variables need to be introduced
    4. Certain methods (thread safety) need to be declared as synchronised.

    Pls. let me know if there should be any other considerations
  2. It's more complicated than that[ Go to top ]

    The following were identified as potential changes...
    All the things you list are important, but might not be sufficient. One essential thing is that you must synchronize properly on all shared resources, including shared objects. In general, this means that using static primitive types are out unless you associate a separate lock with them. If it's an object, you can synchronize on the object before you access it. It's also essential that all shared references and objects be declared volatile (though bear in mind that volatile doesn't work properly in pre 1.4.2 JVMs).

    The big problem is that approaching this change by applying a few simple rules often doesn't work. That is, it's sometimes an extremely difficult problem to make a single-threaded thing into a multi-threaded thing. Synchronization, for example, is essential, but if done incorrectly, can introduce thread-related bugs such as deadlock and nested-monitor lockout. I strongly advise that you have someone with a lot of multithreading experience look at the modified code and make sure that it will work. Hire a consultant if you have to.

    There are a bunch of good books on threading: "Taming Java Threads" (which I wrote) is one, but you should also check out Lewis and Berg's "Multithreaded Programming with Java" and Doug Lea's "Concurrent Programming in Java, 2nd ed." Lewis and Berg's book is a better introduction to the subject, but Lea's book is more thorough, and Lea's library is the basis for the new java.util.concurrent utilities.

    Allen Holub