• 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

Regarding Thread.join()

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The possiblity of getting 1 is 1 in 3000.


Why is the above true?
Thanx
Fabricio.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic