• Post Reply Bookmark Topic Watch Topic
  • New Topic

Parent thread id

 
zaharije pasalic
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

is there any way to find thread's parent thread id?

Thanks
-Zaharije
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends on what you mean by Parent.

Thread constructors do not record the id of the Thread that executes them but they do look at it for a security check and to get priority etc. - see the init() method in Thread source code.

Threads are created as members of ThreadGroup collections, you can certainly find the ids for other members of your group and the ThreadGroup which is the parent of the current one.

Bill
 
zaharije pasalic
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is somehow complex scenarion in background.

We are having axis WebService and using log4j for logging. To simplify testing of our product we creted small wrapper accross log4j so we can provide log data in WS's response. In that way we can check what is logged in that specific request without opening log data.

We decide to intercept log4j's calls and write them into static hash map LOG_DATA<ThreadId, StringBuilder>. We are using ThreadId because we will get same id accross one request.

Here is simplified version of our logging info method:

info(message) {
log4j.info(message)
if (user wanna to log in request) {
LOG_DATA.get(current_thread_id).append(message)
}
}


When reqest finishes we are dunping LOG_DATA into response (using current ThreadId as key).

Now, we are having some issues, because in one point of request we are creating couple of threads to do some paralel processing and main request will wait until they are finished. Problem is now ThreadId - newly created threads are having different thread id's and they will not log into same entry in LOG_DATA.

To resovle this I need somehow to find thread's parent id.

Also there is other problems, because some threads are created from java.lang.Thread, others are from concurrent package, there are others which is created by thord party, ... and i need those logs also - so i cannot simply create ThreadGroup and put all child treads into thath group.


 
Paul Clapham
Sheriff
Posts: 21872
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except for the thread which is created by the JVM to start the application, every thread is created by some other thread. You have decided to call this the "parent thread"; that isn't a concept of the Java language, though.

Thread is like every other class in that a Thread object does not contain a reference to the object which created it. You could write a subclass of Thread whose constructor captures a reference to the current thread when it is run; that would be what you call the "parent thread". Then your problem becomes persuading all those other classes to create instances of your ThreadWhichKnowsItsParent instead of java.lang.Thread.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do know that you can name a Thread when you create it, right?

So just name the Threads that you create with the request Thread id plus some other identifier.

Then each logging call can use the Thread name that executes it.

String s = Thread.currentThread().getName();

Bill
 
zaharije pasalic
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,
thanks for that clarification. It was natural for me (but it is not for java) to have some concept of thread hierarchy.

William,
i was hoping to not need to change other parts of code except logging. But it seems now that i need custom hierarchy.
At the end i will goes with ThreadGroup to put all threads which can log to single group rather than using thread names.

Thanks
-Zaharije
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!