• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

join() method usage

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
you have to use thread2.join() inside thread1.
 
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • 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
  • 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: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • 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
  • 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: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Ankit Garg!
 
Happily living in the valley of the dried frogs with a few tiny ads.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic