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.
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.
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 .