Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

regarding join() method in Thread

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Joining {

static Thread createThread(final int i, final Thread t1){

Thread t2 = new Thread(){
public void run(){
System.out.println(i+1);
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i+2);
}
};

System.out.println(i+3);
t2.start();
System.out.println(i+4);
return t2;
}

public static void main(String[] args) {

createThread(10, createThread(20, Thread.currentThread()));
}
}

can any body explain me the flow of this program?
What does join() actually do here?
Does the static specified for the method 'Joining' makes any change? Because I get different answers if and if not static is present
Plz help me...
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well Well...

(I just came up with some kind of a solution based on the output :-). I am not a 100% sure, but I think I am close)
So think of it like this:
One thing is, the inner method gets executed first. And, I am shortening the name of the method createThread to just "CT", and I am giving the current thread a name, which M (as in main).

Basically, this was the ouput when I ran your program. And please make sure you use the Instant UBB code below when you paste your CODE.

23
24
21
13
14
22
11
12

Hope that helps.

Thanks,
-Vijay
 
Vijay Gade
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The other answer to your question about being static or not is that, I am a little confused. Basically the createThread method was named static so that you can call it within your main method. If you remove the word 'static', your code would not even compile.

Thanks,
-Vijay
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is one of those clearly unguaranteed behaviour of the JVM,if im right!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic