• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Join not working  RSS feed

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

Thread join not working in this case , please help .

I want Thread (Two ) to be executed first and then execute Thread One , but its not happening . Please help .





Please let me know why Thread join isn't working here .

Thanks in advance .
 
Vijitha Kumara
Bartender
Posts: 3973
24
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: Your statement just makes the current thread (which is main) joins the thread referenced by "t1".
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread.join() is working fine. What do you expect it to do here? There are three threads involved in your program: the main thread (the one that's executing main() in the first place), thread t1, and thread t2. (I know you called the other thread "two", but that's inconsistent and silly. I'm going to call it t2.) Anyway, when you call t1.join() from within the main method, which is executed by the main thread, that has the effect of causing the main thread to pause and wait until t1 is complete. It has no effect at all on t2.

If you want to cause t2 to wait for t1 using join(), you would need to insert a call to t1.join() inside the run() method executed by t2. Which would require that you pass a t1 reference to t2, which you don't have right now. More coding, fairly basic stuff, but kind of silly. If you want t2 to wait for t1 to finish, it would be much simpler to just not start t2 until t1 is done. Or don't use start() at all - just call t1.run(), then t2.run(). Here you're not using threads really at all, you're just calling the run() method directly. But that seems fine for what you actually want to do. If you don't want the two threads running at the same time, you probably shouldn't bother using threads at all here.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike ,

I have modified my code , still it isn't working for me








Output :

I Belong to ThrEAD One
I Belong to ThrEAD Two

Please help me to achive this .
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike ,

I have modified my code , still it isn't working for me








Output :

I Belong to ThrEAD One
I Belong to ThrEAD Two

Please help me to achive this .
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

calling join() on a thread has no effect if that thread has not been started yet (strictly - the status of this thread is NEW or TERMINATED).
In this case a call to join simply returns and continues.
modify your code of One class in this way:
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ireneusz Kordal , its working and i got the concept oj joins in Threads .
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!