• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread.currentThread(); and this();  RSS feed

 
subhadeep chakraborty
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am unable to understand the difference between Thread.currentThread() and this(). I have gone through books but i am unable to understand. Any help is appreciated.
Thanks in advance.
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread.currentThread()
Returns a reference to the currently executing thread.

this()
Returns a reference to the currently executing object.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ahmed Yehia:

this()
Returns a reference to the currently executing object.

Well this() refers to the zero-argument constructor of the class where the code is written and it should only appear in a constructor of the same class and also the first statement of the constructor!
However, i think the poster was referring to just the keyword "this"
Yeah, indeed this refers to the instance of the class on which the currently executing method is called. Of course it can not be used inside a static method or block.

Thread.currentThread() and this will refer to the same object if you are inside a non-static method of the currently executing thread.
 
subhadeep chakraborty
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got my question right. Thanks for the correction. My book says with ... (Thread.currentThread() != this) we can test if it is my thread or some arbitrary thread executing my code.. Can you explain this to me .
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!