IntelliJ Java IDE
The moose likes Threads and Synchronization and the fly likes getting thread names Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "getting thread names" Watch "getting thread names" New topic
Author

getting thread names

C hurst
Greenhorn

Joined: Nov 21, 2002
Posts: 4
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");
}


Regards <img src="smile.gif" border="0"> <br />Ciaran
raimondas zemaitis
Ranch Hand

Joined: Feb 23, 2001
Posts: 104
here's how you could try
ThreadReference ref = ((ThreadStartEvent)inEvent).thread();
String name = ref.name();
Vikalp Singh
Ranch Hand

Joined: Dec 29, 2002
Posts: 50
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

Joined: Jul 28, 2002
Posts: 585
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

Joined: Jan 30, 2000
Posts: 18652
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 ]

"I'm not back." - Bill Harding, Twister
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
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.
 
 
subject: getting thread names
 
Threads others viewed
How to add different types of objects to a Vector?
TypeFactory can it be done in java?
getting thread names
How to write junit tests for a method which has LoggingEvent object type as argument
java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture