开发者

How can I get the thread id of the threads of the servlet?

Just as the title. Google didn't give me any clue.

Edit: What I mean is to get the threads that executing the code of the servlet. Thanks.

Edit: Why I want this information, this is because when there are many threads executing, their log aggregate in a single log, and the order of log is disrupt. I want a thread id inserted at the front of each line of the log so that I can trace the activity of each thread.开发者_JAVA技巧


It is not actually a meaningful thing to ask for.

Threads don't belong to a servlet. Rather, they belong to the web container and are used to run requests ... which at certain points involves running servlet methods. A servlet method can of course find out what the current thread is ... but then so can any method.

It is also possible that the web container might use thread groups in a way that allows you to determine that certain threads are used for certain things. But that would be highly implementation specific.

If that's not what you mean, then please refine your question.


What I mean is to get the threads that executing the code of the servlet.

Do you mean currently executing the code of the servlet?

Then I think that the answer is simply - "This is not possible".

It is not possible within a running program for one application thread to find out what code another thread is executing. This kind of thing can only be found using a debug agent ... while all application threads are stopped.


I want a thread id inserted at the front of each line of the log so that I can trace the activity of each thread.

(Well why didn't you simply ask that in the first place??)

A log4j LoggingEvent contains the name of the thread that created the event. You can use a %t in a pattern layout to include the thread name in a log file. You could also write your own custom Appender to filter the events into different "streams" based on the event's thread name.

The thread id is not available for logging ... unless you explicitly insert it into (for example) the log message string.


How about:

long threadId = Thread.currentThread().getId();

in the servlet code? That will give you the ID of the thread running the servlet's service() method. Obviously many of those can be going on simultaneously. Or do you mean something else by "id of the threads of the servlet"?


You can get all the threads in the current thread group with this code:

int active = Thread.activeCount();
Thread allThreads[] = new Thread[active];
active = Thread.enumerate(allThreads);

You can then call getId() for each element of the returned array. You can get fancier by first climbing to the root ThreadGroup of the current thread.

What would you want this information for?


You can get only the currrent thread id using Thread.currentThread().getId(). If you want to track all the threads executing your servlet, keep a list in the request called executedThreads and add the current thread id to the list.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜