File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Thread::setName(string) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Thread::setName(string)" Watch "Thread::setName(string)" New topic
Author

Thread::setName(string)

Aby Krishna
Greenhorn

Joined: Jul 10, 2010
Posts: 14
Hi,
A quick question, what will happen if i invoke setName(String) API on a thread instance after it has been started via "start()". Will the name be at on the thread and printing it would print the new name? or the name can not be set after the thread has been started?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

If the current thread is allowed to access the thread that you are trying to rename, yes.


[My Blog]
All roads lead to JavaRanch
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Don't you try it? Have a look on this API


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
subodh k kumar
Greenhorn

Joined: Aug 19, 2009
Posts: 16

Yes,Inside your run() method you can access the Current thread and reset the name of it.This will print the new Thread Name.
Thread currentThread = Thread.currentThread();
currentThread .setName("newName");

SCJP1.4 , SCWCD 5
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Thread::setName(string)
 
Similar Threads
can we use setPriority inside a synchronized block
Thread names??
How can I get a run() method to return a string variable, when run() method is required to be void ?
Threads
Concurency logic