File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interesting Ques in Threads????? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interesting Ques in Threads?????" Watch "Interesting Ques in Threads?????" New topic
Author

Interesting Ques in Threads?????

Harpal Singh
Ranch Hand

Joined: Oct 10, 2000
Posts: 229
Dear Guys,
Below is a simple programme.....
class FunnyThreads implements Runnable
{
public void run()
{
try
{
for(int i =0;i<3;i++)
{
System.out.println("Child threads "+i);
Thread.sleep(1000);
}
}
catch(InterruptedException ie)
{}
System.out.println("exiting child");
}
public static void main(String[ ] args)
{
FunnyThreads ft = new FunnyThreads();
Thread t = new Thread(ft);
t.start();
try
{
System.out.println("Main thread");
Thread.sleep(500);
}
catch(InterruptedException ie){}
System.out.println("Main Exiting");
}
}
According to some books Like Complete refrence(Patrick)...The main should exit at last else the system will hang...and moreover I think after Main exits there should not be further execution of the programme.....And here is the out put of above programme....
---------- run ----------
Main thread
Child threads 0
Main Exiting
Child threads 1
Child threads 2
exiting child
Normal Termination
Output completed (4 sec consumed).
------
here main() has exited long back...but there is normal termination of the programme.....
CAN SOMEBODY EXPLAIN ME WHY IS THIS SO???
INTRESTING HUH!!!
Thanks,
Harpal
Milind Mahajan
Ranch Hand

Joined: Oct 23, 2000
Posts: 77
Hi,
I will try to answer your question (ofcourse, within the boundaries of my knowledge about threads).
This is what I think is actually happening : When main prints "Main thread" and sleeps, the child is getting CPU. It prints "0". But by this time main gets up and gets the CPU. So it executes it's own code in the main method. Thus, it executes System.out.println("Main exiting") line ( That is how you can see that "main exiting" on the console). Then it waits for the child to finish just before it's own ending curly brace. After the child finishes the main ends and program exits normally.
So, the output "main exiting" is little bit misleading. The main waits after it prints that line!
Hope this helps.
Thanks
Milind
Harpal Singh
Ranch Hand

Joined: Oct 10, 2000
Posts: 229
Milind,
Even I thought the same ...but I think that is not the case...In the programme in main method's for loop try killing the thread ...by inserting this......
Thread.currentThread().interrupt();
And the result is....
---------- run ----------
Main threads 0
Main Interrupted
exiting main
falseChild threads 0
falseChild threads 1
falseChild threads 2
falseChild threads 3
exiting child
Normal Termination
Output completed (3 sec consumed).
Well......There has to be some explaination for this......My doubt is still not solved....
Thanks,
Harpal
Jonathan Jeban
Ranch Hand

Joined: Oct 08, 2000
Posts: 52
Hi Harpal,
The program will continue after main exits if there are still some child threads running.
I am giving this explanation from my notes.(I am not sure its
by Maha or others)

Does a thread continue to run when the program exits the main() method?
Even if the control comes/exits out of the main method, the thread continues to run. As it is main method itself is a seperate thread in Java. Only if u call the exit method explicitly as below, the thread will stop running (All the threads initiated by the program will stop).
public class ThreadRun implements Runnable
{
public void run()
{
for( ; ; )
{
System.out.println("Thread Running");
}
}
public static void main(String a[])
{
Thread t = new Thread( new ThreadRun());
t.start();
System.exit(0);
}
}

Originally posted by Harpal Singh:
Dear Guys,
According to some books Like Complete refrence(Patrick)...The main should exit at last else the system will hang...and moreover I think after Main exits there should not be further execution of the programme.....And here is the out put of above programme....
------
here main() has exited long back...but there is normal termination of the programme.....
CAN SOMEBODY EXPLAIN ME WHY IS THIS SO???
INTRESTING HUH!!!


Hope this helps
Jeban
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Read my answer to an identical post by an identical author
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Harpal Singh
Ranch Hand

Joined: Oct 10, 2000
Posts: 229
Ajith .....Thanks a lot...Clarifies lots of doubts...Your answer makes sense..Jeban,thank you for the answer.....
Harpal
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interesting Ques in Threads?????