This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread getName( ) method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread getName( ) method" Watch "Thread getName( ) method" New topic
Author

Thread getName( ) method

naraharirao mocherla
Ranch Hand

Joined: Aug 16, 2005
Posts: 45
Hi friends
Here ismy question


[/CODE]
class MyThread extends Thread{


String name;
MyThread(String s){
name = s;
System.out.println(this.name);
}

public void run(){
for(int j=0;j<5;j++){

System.out.println(this.getName()+" running");//why 'this.getname()' method is not giving the name of the threads as 'a' and 'b'

//System.out.println(Thread.currentThread().getName());//even this code is not giving the name of the current running thread.just printing Thread 0 and //Thread 1
}
}

public static void main(String [] args) {

MyThread ms1 = new MyThread("a");
MyThread ms2 =new MyThread("b");
ms1.start();//invokes run()
ms2.start();//invokes run()

ms1.getName();//output is 'a'
ms2.getName();//output is 'b'


System.out.println(Thread.currentThread().getName());//output is 'main'

}
}
[CODE]

1.Why cant I use this.getName() method in the run() method.to get the

name odf the current Thread ???

2.Eventhough I'm using Thread.currentThread().getName().I'm not getting

the name of the thread currently executing???

3.when i'm using ms1.getName().i'm getting th eo/p as 'a '//that's fine.

Can sombody expllain this ??

Thanks in advance
Roy Simon
Ranch Hand

Joined: Aug 26, 2005
Posts: 62
dear naraharirao mocherla

you have used a variable 'name' in ur class that subclasses Thread and u have to remmember that there is no variable overridding in java. if u use super("thread name") as ur first line of the constructor the this.getName() method will work well in the run() as u are setting the value of a thread object

since a call to start() calls the THREAD classes run and ur overridden run () method the getName() method will return the name of the super class...
ie ur obj is upcasted to a thread object and is made to run.....
so a call to getName() in the run() method is actually equal to (Thread)myThread1.getName() which returns Thread - 0

whereas in main you invoke the getName() method on a specific subclass object that return the name given by u


hope that answered ur question

regards
simon
naraharirao mocherla
Ranch Hand

Joined: Aug 16, 2005
Posts: 45
Hi RoySimo
n Thanks for your reply.
i tried with super( "ab") in the constructor .But the output is

"ab runnimg "10 times .Even now I'm not getting specific name for each Thread.

1.can you please advise me how to change my code so that I can get the

names of the threads in the for loop'

like

"a running "
"b running "

2.how to change my code so that By using Thread.currentThread().getName()
i can get 'a' and 'b' as thread names instead of Thread 0 and Thread 1.

Thank you very much for your explanation
Kalyana Sundaram
Ranch Hand

Joined: Mar 18, 2005
Posts: 94
Change your code like this...


just insert the call to the superclass constructor with the name as a argument. This super class constructor call would set the Thread's name provided by you while constructing the MyThread Objects. This wiould give you the expected result.


Only those who will risk going too far can possibly find out how far one can go !!!
Roy Simon
Ranch Hand

Joined: Aug 26, 2005
Posts: 62
still no convinced???

ur code should be

class MyThread extends Thread
{
String name = null ;
MyThread( String name )
{
super(name) ;//set the Threads name field to name
this.name = name ;
System.out.println(this.name) ;
}
public void run
{
System.out.println(this.getName())
//dosent matter if u call the Thread class name or myThread name as both
// are the same here thread's name field is printed as there is
// variable overridding
}
public static void main(String[] a)
{
MyThread obj = new MyThread("a") ;
oj1.start() ;
System.out.println(obj.getName()) ; //this prints MyThreads name
}
}

i hope this should blast away your doubts
sorry for not being so descriptive b4

regards
Simon Roy
naraharirao mocherla
Ranch Hand

Joined: Aug 16, 2005
Posts: 45
Thank you very much Roy Simon and Kalyana Sundaram
Thanks a lot .Just calling super(s).
made all the difference .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread getName( ) method