• 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

Interesting Ques in Threads?????

 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read my answer to an identical post by an identical author
Ajith
 
Harpal Singh
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajith .....Thanks a lot...Clarifies lots of doubts...Your answer makes sense..Jeban,thank you for the answer.....
Harpal
 
And will you succeed? Yes you will indeed! (98 and 3/4 % guaranteed) - Seuss. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic