Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getting thread names  RSS feed

 
C hurst
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I'm currently working on a project using JPDA. I have to detect instances where threads are started. This is no problem but i have to determine some way of identifying them uniquely. I was going to use the thread names. However i am having problems getting a handler on the thread that was created. basically in my code i pass an event and detect which type of event this is. If its a threadstartevent i take specific action. however at this point i cannot get a handle on the thread.
public VisEvent(Event inEvent)
{
if (inEvent instanceof MethodExitEvent) {
setMethodType(methodExit);
setEventType(methodExit);
locEvent = (LocatableEvent) inEvent;
}else if (inEvent instanceof MethodEntryEvent){
setEventType(methodEntry);
setMethodType(methodEntry);
locEvent = (LocatableEvent)inEvent;
}else if(inEvent instanceof ThreadDeathEvent){
setEventType(threadDeath);
decrementStackCounter();
}else if(inEvent instanceof ClassPrepareEvent){
setClassTyp(classPrepare);
setEventTyp(classPrepare);
classPrepareEvent((ClassPrepareEvent)inEvent);
}else if(inEvent instanceof ClassUnloadEvent){
setEventType(classUnload);
setClassType(classUnload);
}else if(inEvent instanceof ThreadStartEvent){
//PROBLEM HERE HOW DO I GET THE THREAD NAME
setEventType(threadStart);
incrementStackCounter();
incrementThreadCount();
StackManager threadStack= new StackManager(stackCounter);
}else{
throw new Error("Unexpected event type");
}
 
raimondas zemaitis
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's how you could try
ThreadReference ref = ((ThreadStartEvent)inEvent).thread();
String name = ref.name();
 
Vikalp Singh
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Thread Object we have method getName(), It will return the name of the thread
and for getting currenct thread , we can use
Thread.currentThread()
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DON'T use names. They can be the same. Names do NOT uniquely identify threads. Instead, just save a reference to the Thread:
threadHandles.add( Thread.currentThread() );
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's true names are not guaranteed to be unique identifiers for threads. (Though they usually seem to be unique.) Unfortunately there doesn't seem to be a convenient way to get a reference to the corresponding Thread from a ThreadStartEvent. Thread.currentThread() doesn't do it - the thread that received the ThreadStartEvent is not the thread that started (i.e. not the one we're interested in).

How about:

By the way though - I'm pretty new to JPDA myself. I'm a bit surprised that I can't seem to find a way to get an actual Thread from a ThreadReference. Or more generally, I don't see how to get the corresponding Object from an ObjectReference. Am I just missing something here, or is JPDA designed to prevent you from touching the actual objects involved? I guess this makes sense to prevent the debugger from accidentally interfering with garbage collection for example - but still seems odd to me. Any insight from others is appreciated.
[ February 14, 2003: Message edited by: Jim Yingst ]
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I don't know JPDA at all, but I do know the only way I was able to get my thread manager to be able to get a reference to a thread (including inactive threads!) was to use reflection to get the thread array inside ThreadGroup. Not the best way, BUT the ONLY way to get reference to inactive objects. If you have a threadgroup which you'd like to destroy, the ONLY way to do it is to do the above and call ThreadGroup.remove( inactiveThread | activeThread ) and remove all the threads. You can't destroy a threadGroup if there's an inactive thread still in its array. Personally, i wish they'd thought a bit more about threads and the reasons we need to control them and implemented this all inside threadgroup. then we wouldn't need to touch the thread most of the time, but could do something like:
int pos = Thread.currentThread().getThreadGroupPosition();
then later:
myThreadGroup.interruptThread( pos );
if ( myThreadGroup.threadInactive( pos ) )
myThreadGroup.remove( pos );
or something along those lines.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!