This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to access Threads from their names Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to access Threads from their names" Watch "How to access Threads from their names" New topic
Author

How to access Threads from their names

jon rozanski
Greenhorn

Joined: Dec 04, 2012
Posts: 5
Here (name referencing threads) they mention a way to reference threads by their name, but yes, it seems much easier to do this with an array
Steve Dambrosio
Greenhorn

Joined: Apr 02, 2009
Posts: 28
jon rozanski wrote:Here (name referencing threads) they mention a way to reference threads by their name, but yes, it seems much easier to do this with an array


Yes, you can name threads, but I don't see how that helps you to reference a thread. I've seen thread names used to identify individual threads in log messages, for example. If I want a thread to do my bidding, then I need a reference to it, and that would be a variable, not the thread's name.
jon rozanski
Greenhorn

Joined: Dec 04, 2012
Posts: 5


On that link the show this to reference a thread by name
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
Welcome to the Ranch

That does not look a very efficient way to get threads by name. And why would you want to?
It might be better to put the threads and their names (or more precisely the other way round) into a Map<String, Thread>. But again, why would you want to do it?

This appears to be a new question on an old discussion; please don’t do that. I can create a new discussion for the new question, fortunately.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
This is the old discussion.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Yes, you could do that. But in my opinion if you expect that you're going to have to work with an object, your design should just keep a reference to that object, instead of trying to scavenge it back from the environment later. You have there a hack which works with Thread objects, but not with any other kind of objects.

And likewise if you're changing your design and you find you have to work with an object which you don't have a handy reference to, then you should change the design in such a way that you start keeping that reference around.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to access Threads from their names
 
Similar Threads
Sams teach yourself java help
� Is it possible to write a Java program that accumulates memory over time?
EventHandler as ActionListener usage
open URL
need help