Documente Academic
Documente Profesional
Documente Cultură
Evidence 4 Main Thread dead after main method exits (SCJP forum at JavaRanch)
File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Java FAQ
Recent Topics
Register / Login
Author
Jeganathan Swaminathan Greenhorn Joined: Dec 12, 2000 Posts: 5
Hi all, I read Maha's discussions, I found her discussions is really useful and great, but in the Threads topic i found her saying that Main thread will wait till all its child threads dies . But the program given below shows clearly contradicts, may be i'm wrong, so i want comments on this as i'm taking the SCJP2 within 2 weeks.
view plain
c opy to c lipboard
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 .
p u b l i cc l a s sA f t e r A l l T h r e a d P r o g r a me x t e n d sT h r e a d { s t a t i cT h r e a dt 1 ; p u b l i cv o i dr u n ( ) { S y s t e m . o u t . p r i n t l n ( " T h r e a dG r o u pN a m ei s"+T h r e a d . c u r r e n t T h r e a d ( ) . g e t T h r e a d G r o u p ( ) ) ; w h i l e ( t r u e ) { S y s t e m . o u t . p r i n t l n ( " C h i l dT h r e a d"+T h r e a d . c u r r e n t T h r e a d ( ) . g e t N a m e ( ) ) ; S y s t e m . o u t . p r i n t l n ( t 1 . i s A l i v e ( ) ) ; / / P r i n t sf i r s tt r u ei n d i c a t i n gm a i ni sa l i v e .T h i sw i l l / / c o n t i n u ea t l e a s t1s e c .A f t e rt h a ti tp r i n t sf a l s e / / T h i sm e a n st h a tm a i nt h r e a di sd e a d . S y s t e m . o u t . p r i n t l n ( " T o t a lT h r e a d s "+T h r e a d . c u r r e n t T h r e a d ( ) . a c t i v e C o u n t ( ) ) ; / / H e r et h ea c t i v e c o u n ta l w a y sp r i n t s2W h y ? ? ?I ti ss u p p o s et ob e1a sp e rm yk n o w l e d g e } } p u b l i cs t a t i cv o i dm a i n ( S t r i n ga r g s [ ] )t h r o w sI n t e r r u p t e d E x c e p t i o n { S y s t e m . o u t . p r i n t l n ( T h r e a d . c u r r e n t T h r e a d ( ) . g e t N a m e ( ) +"i st h eM a i nT h r e a d&s t a r t e de x e c u t i n g " ) ; S y s t e m . o u t . p r i n t l n ( " T h r e a dG r o u pN a m ei s"+T h r e a d . c u r r e n t T h r e a d ( ) . g e t T h r e a d G r o u p ( ) ) ; S y s t e m . o u t . p r i n t l n ( " T o t a lT h r e a d si nt h i sT h r e a d G r o u pi s"+T h r e a d . c u r r e n t T h r e a d ( ) . a c t i v e C o u n t ( ) ) ; / / H e r ei ti sp r i n t i n g1a n di ti sc o r r e c ts i n c eo n l ym a i nt h r e a di sa l i v e t 1=T h r e a d . c u r r e n t T h r e a d ( ) ; T h r e a dt=n e wA f t e r A l l T h r e a d P r o g r a m ( ) ; S y s t e m . o u t . p r i n t l n ( " T o t a lT h r e a d si nt h i sT h r e a d G r o u pi s"+T h r e a d . c u r r e n t T h r e a d ( ) . a c t i v e C o u n t ( ) ) ; / / H e r ei ti sp r i n t i n g2 e v e nb e f o r es t a r t i n gc h i l dt h r e a d ,w h y ? ? ? / / m ys u r p r i s ei sw h e t h e rc h i l dt h r e a di sa c t i v ee v e nb e f o r es t a r t i n gi t .
www.coderanch.com/t/196070/java-programmer-SCJP/certification/Evidence-Main-Thread-dead-main
1/4
7/14/13
3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 .
/ / m ys u r p r i s ei sw h e t h e rc h i l dt h r e a di sa c t i v ee v e nb e f o r es t a r t i n gi t . t . s t a r t ( ) ; s l e e p ( 1 0 0 0 ) ; S y s t e m . o u t . p r i n t l n ( " M a i nM e t h o dt e r m i n a t e d " ) ; } }
Evidence 4 Main Thread dead after main method exits (SCJP forum at JavaRanch)
Adrian Yan Ranch Hand Joined: Oct 02, 2000 Posts: 688 Ajith Kallambella Sheriff Joined: Mar 17, 2000 Posts: 5782
Main thread does not wait for all children threads. Main thread by default is a daemon thread.
Adrian, Main thread is not a daemon thread, it is a user thread. The main ThradGroup is not daemon either. Take a look at the following code( and read the comments ) which proves my point.
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 .
Open Group C ertified Distinguished IT Architect. Open Group C ertified Master IT Architect. Sun C ertified Architect (SC EA). Jeganathan Swaminathan Greenhorn Joined: Dec 12,
www.coderanch.com/t/196070/java-programmer-SCJP/certification/Evidence-Main-Thread-dead-main
I agree that main thread is not a Daemon thread but can anyone comment about Whether main thread exits when main method terminates? And thanks a lot 4 ur replies.
2/4
7/14/13
Joined: Dec 12, 2000 Posts: 5
Evidence 4 Main Thread dead after main method exits (SCJP forum at JavaRanch)
method terminates? And thanks a lot 4 ur replies.
Yes Jeganathan main thread dies before the child thread in your case. I too had this confusion but after reading khalid I'm satisfied. Read page no. 273. My doubt originated from the program that I tried. Here's it. // program to demonstrate main thread dies before the child thread. class ChildThread extends Thread { Thread mainThread; ChildThread(Thread t) { mainThread = t; } public void run() { System.out.println("Main thread is alive at the start of run meth() : " + mainThread.isAlive()); try { for(int i =0;i<5;i++) { sleep(500); System.out.println("Child Thread counting : " + i); System.out.println("Main thread is alive in the run meth() : " + mainThread.isAlive()); } } catch(InterruptedException r){} System.out.println("Main thread is alive at the end of run meth() : " + mainThread.isAlive()); } } public class MainThreadIsAlive { public static void main(String a[]) { Thread t = Thread.currentThread(); System.out.println("Main thread is alive at the start of main meth() : " + t.isAlive()); ChildThread child = new ChildThread(t); child.start(); System.out.println("Main thread is alive at the end of main meth() : " + t.isAlive()); } } -vadiraj
Regards<BR>---------<BR>vadiraj<P><BR>*****************<BR>There's a lot of I in J.<BR>*****************
Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
Similar Threads Main thread terminates when main method exits threading issue Thread Communication - Starting and Stopping the threads status of deamon threads Six java questions: Could anyone answer
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 14, 2013 08:36:35 .
www.coderanch.com/t/196070/java-programmer-SCJP/certification/Evidence-Main-Thread-dead-main
3/4
7/14/13
Evidence 4 Main Thread dead after main method exits (SCJP forum at JavaRanch)
Contact Us | Powered by JForum |
C opyright 1998-2013 Paul W he aton
www.coderanch.com/t/196070/java-programmer-SCJP/certification/Evidence-Main-Thread-dead-main
4/4