Multithreading in servlets..!!!!!


Web tier: servlets, JSP, Web frameworks: Multithreading in servlets..!!!!!

  1. Multithreading in servlets..!!!!! (3 messages)

    Hi All,

     I have a requirement by my client,rather peculiar it is that, he wants

    Multithreading inside servlet to retrive the data from the DB.
    I wrote a servlet with Multithreading in servlet like this(sample code below). and i am suppose

    to deploy this in websphere3.5.

    public Vector processRequest(HttpServletRequest request,

    HttpServletResponse response)
          ThreadGroup thGrp = new ThreadGroup("ThGrp");
     Following are 4 classes which run as Threads with run method inside. to retrive data frm DB

          ThreadExample cl1 = new ThreadExample();
          ThreadExample2 cl2 = new ThreadExample2();
          ThreadExample3 cl3 = new ThreadExample3();
          ThreadExample4 cl4 = new ThreadExample4();
          Thread t1 = new Thread(thGrp,cl1);
          Thread t2 = new Thread(thGrp,cl2);
          Thread t3 = new Thread(thGrp,cl3);
          Thread t4 = new Thread(thGrp,cl4);
              if(thGrp.activeCount() == 0){
         try {

                   HERE I DO THE GET ALL DATA.. FROM ALL hreads.
                  }catch( io){

    But when I checked the performance it takes exactly double the time compared to My simple servlet.The simple servlet does the work of 4 Clases mentioned above

    sequentially. Now i want to know CAN I do something like above.?. If yes,

    how i can get better performance with this?.

    Thnx in advance,
  2. Well, if you really did something like
    while (true)
      if (threadGroup.activeCount()== 0) {
        // do work here

    Then you are wasting a lot of CPU cycles just looping around. You can replace the entire loop by:

    // do work here

    Join waits for the thread to finish, then returns. The code above will simply wait until all four threads finish, and then continue. Unlike your code, this code does not waste CPU cycles.
    Another problem is that you create new threads for each request. Creating a thread is a potentially expensive operation. You would be better off using a thread pool. You can either check your App server docs to find out if it provides one, or prefferably, use some standard solution. I think the Apache "commons" project has a thread pool (if not then it's another Apache project).

    Even if you do use this technique, there is no guarantee that the code will run faster. It depends on many factors, including the number of processors on your machine, the type of operations each thread performs (i.e, IO or calculations), the VM itself, etc. Just because you use threads does not mean your code will be faster.

  3. Hi Gal,

    Thnx, for ur help. let me test this.

  4. Hi,

    You can do this, but the question is: "Why do you want to do something like this?"

    Creating Threads is a really expensive operation, not to mention all the synchonsation mechanisms. Most servers are bypassing these problems by using a pool of threads and other reusable objects.

    When you have to use multipe threads, you may consider using a pool too.