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

this.join  RSS feed

 
Sandeep Jindal
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does it mean to have this.join

e.g.

class MyThread extends Thread{
public void run(){
//something
abc();
}

public void abc(){
this.join();
//something
}

In general t.join would mean that in the current thread, let thread t complete (does complete here mean end run method or it could be block/etc) and then run the lines below t.join().
What would it mean for this.join?
}
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult a question for beginners. Moving.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) It won't compile as you don't catch InterruptedException

2) It will block until another thread (or JVM) interrupts it ie the thread completing execution is not the only way out of the join.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know that any of the above really answers the question. Calling a method in a thread subclass that includes "this.join()" does the same thing that calling "join()" directly on the thread does. Remember that the thread that calls abc() does not have to be the Thread subclass we're looking at, and in fact, such a method call wouldn't make any sense. But if some other thread calls abc(), then it's going to wait until this thread is done running. Presumably abc() has some other code in it too, so it's probably "send some data, and wait for the thread to terminate", or something like that.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read the question differently to you perhaps we need some clarification and the real example, I see your point that abcd is public and this might be a hint that another thread might access it but also note the run does call it.

I assumed the this.join vs join was just coding style rather than confusion on the part of the author, which is interesting as this.join is supposed to make things clearer ;-) rather than confuse as to which this.

I assumed the actual code was either going to be an exam question trying to throw you or some hacky demo code where they wanted some daemon threads to go do something and have a main user thread block till interrupted or the like (not a good idea)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!