• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

join() method usage

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This code makes the current thread (main method) to execute after the thread2 finishes its job. How to make thread2 to execute after thread1?









 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you have to use thread2.join() inside thread1.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harpreet Singh janda wrote:you have to use thread2.join() inside thread1.


Not exactly, if you want to start thread2 after thread1 completes, then you'll have to change the lines 34 and 35 i.e.
to
If you want thread2 to wait for thread1 to finish but main thread to complete without interruptions, then thread2 needs a reference to thread1 and call the join method on that reference...
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:

Harpreet Singh janda wrote:you have to use thread2.join() inside thread1.


Not exactly, if you want to start thread2 after thread1 completes, then you'll have to change the lines 34 and 35 i.e.
to
If you want thread2 to wait for thread1 to finish but main thread to complete without interruptions, then thread2 needs a reference to thread1 and call the join method on that reference...



See the coding :


I make it to Thread1 - first, Thread2 - second, Main - 3. But in some cases, I didn't get the whole Output of Thread1?

 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But in some cases, I didn't get the whole Output of Thread1?


I don't know how you created and compiled the program, but for me thread1's output always completes before thread2 starts...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:

But in some cases, I didn't get the whole Output of Thread1?


I don't know how you created and compiled the program, but for me thread1's output always completes before thread2 starts...



That's a mistake of mine.... Sorry....

Ankit wrote :


If you want thread2 to wait for thread1 to finish but main thread to complete without interruptions, then thread2 needs a reference to thread1 and call the join method on that reference...



Hi, You mean this.....

 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abimaran, I didn't exactly mean the same thing but its almost the same as your code. Your code is also going to stop the main method till thread1 completes, because you have a join call in the main method. Even if you remove that join call, the main method will wait for thread1 to complete because you called join in the constructor of Thread2 class. I was talking about this
Now the main method will complete without any waiting and thread2 will only start when thread1 is complete...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit Garg!
 
You totally ruined the moon. You're gonna hafta pay for that you know. This tiny ad agrees:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic