Forums Register Login

Thread.currentThread(); and this();

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thread.currentThread()
Returns a reference to the currently executing thread.

this()
Returns a reference to the currently executing object.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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 .
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 855 times.
Similar Threads
explanation needed.
Constructor calling
How to get the servlet's requesting page ?
Theta Join
priorityQuery problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:58:34.