You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "prs05@prosa.com" <pr...@prosa.com> on 2001/10/16 14:12:31 UTC

Active servlet threads?

Hi!
  I'd like to know the number of currently active threads of a servlet from within that very servlet... I don't know if I've explained well: I'd like to make a method that counts the active threads, to know in a certain moment how many "servlet copies" are running. I'll use this da<tum to determine if there aren't anyone, and so I can stop the service (or do anything else) without anyone complaining and without any delay in waiting the other threads to finish.
  Is it possible? I tried with Thread.CurrentThread().getThreadGroup().activeCount(), but it gives me 32, that I guess is the number of the pooled threads, of all the servlet, not only this very one...
  may you help me? Thanks!
   Germano

Re: Active servlet threads?

Posted by Bo Xu <bo...@cybershop.ca>.
"prs05@prosa.com" wrote:

> Hi!
>   I'd like to know the number of currently active threads of a servlet from within that very servlet... I don't know if I've explained well: I'd like to make a method that counts the active threads, to know in a certain moment how many "servlet copies" are running. I'll use this da<tum to determine if there aren't anyone, and so I can stop the service (or do anything else) without anyone complaining and without any delay in waiting the other threads to finish.
>   Is it possible? I tried with Thread.CurrentThread().getThreadGroup().activeCount(), but it gives me 32, that I guess is the number of the pooled threads, of all the servlet, not only this very one...
>   may you help me? Thanks!
>    Germano

the following is as a refrence :-)


public class MyServlet extends HttpServlet{
    private static byte[] locker=new byte[0];
    private static int counter;

    public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{
          synchronized(locker){
              counter++;
         }
    }
}



RE: Active servlet threads?

Posted by Jim Urban <ji...@netsteps.net>.
This is primitive, but should work...

1. Declare a static int counter within your servlet class (default to 0).
private static int useCount = 0;

2. Wrap your entire doGet() and doPost() in a try block.

3. The first thing in the try block would be to increment the counter.

4. The last thing in your finally block would be to decrement the counter.

5. You could then add a static public method to return the counter.
public static int getUseCount(){
  return useCount;
}

Hope this helps.

Jim

-----Original Message-----
From: prs05@prosa.com [mailto:prs05@prosa.com]
Sent: Tuesday, October 16, 2001 7:13 AM
To: tomcat-user@jakarta.apache.org
Subject: Active servlet threads?


Hi!
  I'd like to know the number of currently active threads of a servlet from
within that very servlet... I don't know if I've explained well: I'd like to
make a method that counts the active threads, to know in a certain moment
how many "servlet copies" are running. I'll use this da<tum to determine if
there aren't anyone, and so I can stop the service (or do anything else)
without anyone complaining and without any delay in waiting the other
threads to finish.
  Is it possible? I tried with
Thread.CurrentThread().getThreadGroup().activeCount(), but it gives me 32,
that I guess is the number of the pooled threads, of all the servlet, not
only this very one...
  may you help me? Thanks!
   Germano


Re: Active servlet threads?

Posted by Dmitri Colebatch <di...@bigpond.net.au>.
I'm not sure its really going to achieve what you want, but... if you had
something like:

  private int threadCount = 0;

  public void doGet( ... )
  {
    threadCount++;

    // process request, print out threadCount

    threadCount--;
  }

it should do the trick - assuming I've understood what you want.  

However, wouldn't the log files provide just as easy (and more
appropriate) access to ths information?

cheers
dim

On Tue, 16 Oct 2001, prs05@prosa.com wrote:

> Hi!
>   I'd like to know the number of currently active threads of a servlet
> from within that very servlet... I don't know if I've explained well:
> I'd like to make a method that counts the active threads, to know in a
> certain moment how many "servlet copies" are running. I'll use this
> da<tum to determine if there aren't anyone, and so I can stop the
> service (or do anything else) without anyone complaining and without
> any delay in waiting the other threads to finish.
>   Is it possible? I tried with
> Thread.CurrentThread().getThreadGroup().activeCount(), but it gives me
> 32, that I guess is the number of the pooled threads, of all the
> servlet, not only this very one...
>   may you help me? Thanks!
>    Germano
>