Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Servlet Threads Question

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Thanked 0 Times in 0 Posts

    Default Servlet Threads Question

    I'll just start by saying any assistance on this issue is greatly appreciated.

    So, Iím writing a Java Servlet that receives JSON messages contained in a HTTP POST, parses the JSON message to an object and writes the various elements in the object to a file in a single directory. Iíve got this working fine.

    The problems arise when I modify the sender so it can send multiple requests at the same time. My understanding is that to handle this the Servlet will initiate threads to deal with the requests. To allow the Servlet to do this I need to make the Servlet thread safe. This means no global Servlet variables that are modified in local doPost functions. The problem is I need to set a limit on how many JSON messages are written to files (for example 100). At the moment I have a variable that is global to the Servlet that increments each time a message is read in. This is fine. If we make the Servlet thread safe this variable can no longer be incremented by each thread so my question is is it possible to have a variable that is persisted by a thread and can be accessed the next time around so we know how many events have been written to the file? Or is my understanding incorrect and each thread will deal with the POST and for the next request a new thread is created so nothing can be persisted?

    Obviously an alternative would be to get each thread to look at its directory and say is there a file? Is it over a maximum file size? If not, write to it. But I was hoping to speed things up by just having a file open that can be written to until a counter is reached. FYI I need each thread to write to its own file in its own directory which it will get from a pool of directories.

    I might be attempting the impossible, or going about it the wrong way.

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 833 Times in 772 Posts
    Blog Entries

    Default Re: Servlet Threads Question

    First off, do some searches on threads and servlets. Depending upon where you get the information, threads in this context can be discouraged or not (see Constant Servlet Running for a good discussion on the subject). A brief overview of the servlet spec doesn't seem to mention anything beyond making sure things are thread safe. That out of the way I'd suggest looking into creating a single thread pool which manages the requests as a queue, so you don't have the overhead of creating so many different threads, have a single manager that manages one or more threads in a queue. Store this as a Session variable, so you can retrieve it from HTTPServletRequest, and pass a reference of the Session to the manager, which will allow you to store the count as a session variable (always access the the Session from your threads in a synchronized way). Finally, create a test structure and hammer at it to be sure doing all of the above results in correct behavior.
    Last edited by copeg; October 21st, 2010 at 09:23 AM.

Similar Threads

  1. newbie help threads
    By fortune2k in forum Threads
    Replies: 19
    Last Post: October 20th, 2010, 02:40 PM
  2. Working with threads
    By tccool in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 12th, 2010, 10:21 AM
  3. [SOLVED] Questions About Threads
    By neo_2010 in forum Threads
    Replies: 4
    Last Post: March 15th, 2010, 09:04 AM
  4. threads in gui
    By urosz in forum AWT / Java Swing
    Replies: 1
    Last Post: November 3rd, 2009, 05:20 PM