*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Thread.join() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Thread.join()" Watch "Regarding Thread.join()" New topic
Author

Regarding Thread.join()

Chandra Sekhar
Ranch Hand

Joined: Sep 26, 2003
Posts: 90
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.


"Luck is when opportunity meets preparation"
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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!


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Kashif Memon
Ranch Hand

Joined: Jul 12, 2003
Posts: 35
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

Joined: Aug 05, 2002
Posts: 787
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

Joined: Mar 11, 2003
Posts: 33
The possiblity of getting 1 is 1 in 3000.

Why is the above true?
Thanx
Fabricio.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Regarding Thread.join()