This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

regarding join() method in Thread

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
This is one of those clearly unguaranteed behaviour of the JVM,if im right!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!