• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Query about start() and join()

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand that join will ask the current running thread to wait for some time till the thread on which join is invoked gets completed .but if join is invoked on the same thread which was started earlier.then what happens?can anybody please explain that??
Here is the code snippet

public class ProjectNew implements Runnable {
public static void main(String[] args) throws Exception {
Projectnew n=new Projectnew();
Thread t = new Thread(n);
t.start();
System.out.print("Test 1");
t.join();
System.out.print("Test2");
}

public void run() {
System.out.println("Test 3");
}
}
Please explain how this code works??what actually happens when start and join is called??
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your code, you are starting a thread, and then calling join on it.

When you start thread t, there are actually two threads - main thread and t. Now, you are calling t.join() inside main thread. Here, main thread goes in waiting state till t thread finishes. Once t finishes, main will continue.

I hope this helps.
 
gunjan khanuja
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes ..thanks..now i hav understood.Problem is resolved.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome.
 
A timing clock, fuse wire, high explosives and a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic