• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Parent thread id

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

Thanks
-Zaharije
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic