Sunteți pe pagina 1din 4

7/14/13

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

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Certification Programmer Certification (SCJP/OCPJP)

Author
Jeganathan Swaminathan Greenhorn Joined: Dec 12, 2000 Posts: 5

Evidence 4 Main Thread dead after main method exits


posted 12/14/2000 12:22 PM

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

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 .

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)

with regards, Jeganathan

S.Jeganathan MC A<BR>Software Engg.<BR>PSI Data Systems Ltd.,<BR>Bangalore.<BR>jeganath@psi.soft.net

Adrian Yan Ranch Hand Joined: Oct 02, 2000 Posts: 688 Ajith Kallambella Sheriff Joined: Mar 17, 2000 Posts: 5782

posted 12/14/2000 8:47 PM

Main thread does not wait for all children threads. Main thread by default is a daemon thread.

posted 12/14/2000 9:02 PM

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 .

/ * L o g i cW h e nc o d er u n n i n gi ns o m et h r e a dc r e a t e san e wT h r e a do b j e c t , t h en e wt h r e a dh a si t sp r i o r i t yi n i t i a l l ys e te q u a lt ot h e p r i o r i t yo ft h ec r e a t i n gt h r e a d ,a n di sa d a e m o nt h r e a di fa n do n l yi ft h ec r e a t i n gt h r e a di sad a e m o n . * / p u b l i cc l a s sI s M a i n D a e m o n { p u b l i cs t a t i cv o i dm a i n (S t r i n g [ ]s ) { / /C r e a t ean e wt h r e a do b j e c t .T h i st h r e a dn o wb e l o n g s / /t ot h es a m eT h r e a d G r o u pa st h em a i nt h r e a da n dh e n c e / /i n h e r i t st h ep r o p e r i t i e ss u c ha sp r i o r i t ya n dd a e m o n n e s s T h r e a dt=n e wT h r e a d ( ); / /T h i sl i n ew i l la f f i r mt h a tt h i sn e wt h r e a da n dt h em a i n / /t h r e a db e l o n g st ot h es a m eT h r e a d G r o u pi e . ," m a i n " S y s t e m . o u t . p r i n t l n (t . g e t T h r e a d G r o u p ( ) ) ; / /I st h et h r e a dg r o u po ft h en e wt h r e a dd a e m o n ? / /R e m e m b e rm a i nb e l o n g st ot h es a m et h r e a dg r o u pt o o ! S y s t e m . o u t . p r i n t l n (t . g e t T h r e a d G r o u p ( ) . i s D a e m o n ( ) ) ; / /I st h en e wt h r e a di t s e l fd a e m o n ? ? / /T h i sa f f i r m st h a tt h en e wt h r e a dr e a l l yi n h e r i t st h e / /d a e m o n n e s sf r o mt h ep a r e n tT h r e a d G r o u p / T h r e a d .S i n c ew e / /h a v en oe x p l i c i t l ys e tt h en e wt h r e a da sd a e m o n ,t h ef o l l o w i n g / /l i n es h o u l dr e t u r nf a l s e . S y s t e m . o u t . p r i n t l n (t . i s D a e m o n ( ) ) ; } }

Hope that helps, Ajith

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,

posted 12/15/2000 9:42 AM

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.

vadiraj vd Ranch Hand Joined: Dec 15, 2000 Posts: 68

posted 12/15/2000 4:59 PM

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

subject: Evidence 4 Main Thread dead after main method exits

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

S-ar putea să vă placă și