This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Thread.join()

 
Chandra Sekhar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I found a question on Threads from a mock exam.
class A extends Thread {
private int i;
public void run() {i = 1;}
public static void main(String[] args) {
A a = new A();
try{
a.start();
a.join(); //Line 1
System.out.print(a.i);
}
catch(Exception ex){}
}}

The out put is 1.But if I comment Line 1,the out put is 0.
Can some one explain what difference it makes when a call to join() is made.
Thanks
Chandrasekhar S.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chandru_s,
Welcome to Javaranch, a friendly place for Java greenhorns
We ain't got many rules 'round these parts, but we do got one. Please change your displayed name to comply with the JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Kashif Memon
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At line 1 the main thread waits for thread 'a' to complete its process, but if u comment line 1, main thread continues its execution with out waiting for thread 'a' and results in a.i=0.
if u comment line 1, possible out is:
0 or 1.
if u do not comment line 1, possible out is:
1
Regards,
Kashif Memon
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
posted by Kashif Memon:
if u comment line 1, possible out is:
0 or 1.

The possiblity of getting 1 is 1 in 3000.
 
Fabricio Sanchez
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The possiblity of getting 1 is 1 in 3000.

Why is the above true?
Thanx
Fabricio.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic