Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why doesnt the run() method have reference to the calling thread  RSS feed

 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain the sentence that the run method doesnt have reference to the calling thread
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The run method is a simple method with no arguments, why should it have a special reference to it's running Thread? You can always get one via Thread.currentThread() if you wanted one.
 
Sergey Babkin
author
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Ragi is asking about the thread that called start(). There is no good reason to have this reference. There is no caller-callee relationship between threads. Not even a parent-child relationship like between the OS processed. One thread starts another and then may do whatever it pleases, including exiting. Having that reference would mean that if that thread exited, it could not be garbage collected until the other thread exits and gets garbage-collected too.

If you want, you can always add this manually by giving it as the argument to your thread constructor and remember it in the thread (subclass) object.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!