aspose file tools
The moose likes Threads and Synchronization and the fly likes Thread.currentThread(); Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Thread.currentThread();" Watch "Thread.currentThread();" New topic
Author

Thread.currentThread();

sudharani Savadi
Ranch Hand

Joined: Feb 23, 2011
Posts: 30

Can anyone help me for understanding Thread.currentThread() method??.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 3859
    
    1

Hi Sudharani. Welcome to The Ranch!

Well, the best place to start is the documentation for the Thread class. Which says:
Returns a reference to the currently executing thread object.


currentThread() is a static method of Thread (which is why you don't need a reference to a Thread object to call it). It returns a reference to a Thread object, and it will always return a reference to the Thread that it's executed in.

Is that clear enough?
sudharani Savadi
Ranch Hand

Joined: Feb 23, 2011
Posts: 30

Still I am getting confusion about Thread name because Thread[main,5,main], it contains two mains so I am not getting which one is thread name and main method name. can you tell me in detail?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 3859
    
    1

sudharani Savadi wrote:Still I am getting confusion about Thread name because Thread[main,5,main], it contains two mains so I am not getting which one is thread name and main method name. can you tell me in detail?

I assume you're getting that by printing the value of a Thread (which will use the toString() method)? Well, to be sure you could create your own Thread object, give it a different name, and print that out. But you can get an indication again by checking the documentation. From Thread.toString():
Returns a string representation of this thread, including the thread's name, priority, and thread group.

So the likelihood is that the first "main" is the name, and the second "main" is the thread group name. But trying your own experiment is the best way to be certain.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Thread.currentThread();
 
Similar Threads
Is there a way to check the methods calling another method
Getting reference to active threads
unable to get thread name
Caller method and line
Threads