• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread.currentThread();

 
sudharani Savadi
Ranch Hand
Posts: 30
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone help me for understanding Thread.currentThread() method??.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!