Vous êtes sur la page 1sur 4

7/14/13

[ A good finding ] - User & Deamon Threads (SCJP forum at JavaRanch)

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

[ A good finding ] - User & Deamon Threads


posted 3/12/2004 6:00 PM

Vijay Pawar Greenhorn Joined: Sep 04, 2003 Posts: 26

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 .

p u b l i cc l a s sT h r e a d T e s t { p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ) { S y s t e m . o u t . p r i n t l n ( " T h i si sm a i nt h r e a d " ) ; T h r e a dt=n e wC h i l d T h r e a d ( ) ; t . s t a r t ( ) ; S y s t e m . o u t . p r i n t l n ( " n o we n d i n gm a i n " ) ; } } c l a s sC h i l d T h r e a de x t e n d sT h r e a d { p u b l i cv o i dr u n ( ) { f o r ( i n ti=1 ;i <2 0 ;i + + ) { 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 :"+i ) ; t r y { T h r e a d . s l e e p ( 5 0 0 ) ; } c a t c h ( E x c e p t i o ne ) { } } }


1/4

www.coderanch.com/t/244930/java-programmer-SCJP/certification/good-finding-User-Deamon-Threads

7/14/13

[ A good finding ] - User & Deamon Threads (SCJP forum at JavaRanch)

2 1 .

The above code completes the ChildThread even if the main method completes earlier. The program ends only when the ChildThread completes its run method. Here the ChildThread created is a user thread. If we call t.setDaemon(true) before calling t.start then the ouptput is : C:\test>java ThreadTest This is main thread now ending main Child Thread: 1 This means that the child thread does not complete the run method and is killed when the main method terminates.
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 .

p u b l i cc l a s sT h r e a d T e s t { p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ) { S y s t e m . o u t . p r i n t l n ( " T h i si sm a i nt h r e a d " ) ; C h i l d T h r e a dt=n e wC h i l d T h r e a d ( ) ; t . s o m e n a m e=" d a e m o n "; t . s e t D a e m o n ( t r u e ) ; t . s t a r t ( ) ; C h i l d T h r e a dt 1=n e wC h i l d T h r e a d ( ) ; t 1 . s o m e n a m e=" u s e r " ; t 1 . s t a r t ( ) ; S y s t e m . o u t . p r i n t l n ( " n o we n d i n gm a i n " ) ; } } c l a s sC h i l d T h r e a de x t e n d sT h r e a d { p u b l i cS t r i n gs o m e n a m e=n u l l ; p u b l i cv o i dr u n ( ) { f o r ( i n ti=1 ;i <2 0 ;i + + ) { 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 :"+s o m e n a m e + ": " +i ) ; t r y { T h r e a d . s l e e p ( 5 0 0 ) ; } c a t c h ( E x c e p t i o ne ) { } } } }

In the above example, t is a daemon thread and t1 is a user thread. Even if main method completes, since t1 is a user thread it will continue to execute. Hence in this case the daemon thread t will not be killed. The daemon thread is killed by the JVM only if its parent thread is killed. The killing is invoked by the completion of parent thread. But since the user thread t1 is alive, the parent main thread will remain alive ( program will not terminate ) till the user thread completes.
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

www.coderanch.com/t/244930/java-programmer-SCJP/certification/good-finding-User-Deamon-Threads

2/4

7/14/13

[ A good finding ] - User & Deamon Threads (SCJP forum at JavaRanch)

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 . 3 4 . 3 5 . 3 6 .

p u b l i cc l a s sT h r e a d T e s t { p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ) { S y s t e m . o u t . p r i n t l n ( " T h i si sm a i nt h r e a d " ) ; D a e m o n T h r e a dt=n e wD a e m o n T h r e a d ( ) ; t . s e t D a e m o n ( t r u e ) ; t . s t a r t ( ) ; U s e r T h r e a dt 1=n e wU s e r T h r e a d ( ) ; t 1 . s t a r t ( ) ; S y s t e m . o u t . p r i n t l n ( " n o we n d i n gm a i n " ) ; } } c l a s sU s e r T h r e a de x t e n d sT h r e a d { p u b l i cv o i dr u n ( ) { f o r ( i n ti=1 ;i <2 0 ;i + + ) { S y s t e m . o u t . p r i n t l n ( " U s e rT h r e a d :" +i ) ; t r y { T h r e a d . s l e e p ( 5 0 0 ) ; } c a t c h ( E x c e p t i o ne ) { } } } } c l a s sD a e m o n T h r e a de x t e n d sT h r e a d { p u b l i cv o i dr u n ( ) { f o r ( i n ti=1 ;i <2 0 ;i + + ) { S y s t e m . o u t . p r i n t l n ( " D a e m o nT h r e a d :" +i ) ; t r y { T h r e a d . s l e e p ( 1 0 0 0 ) ; } c a t c h ( E x c e p t i o ne ) { } } } }

In this example the UserThread counts upto 20 but the Daemon Thread counts only upto 10 and then by this time the User thread has completed ( 500 ms sleep time) and also the main thread has completed, hence now there are no more user thread alive and hence the daemon will be killed forcefully by the JVM.
Narasimha Rao B. Ranch Hand Joined: Aug 26, 2002 Posts: 205

posted 3/12/2004 6:57 PM

Hi Jughead, Nice examples and explanations.... Thanks...

Narasimha

I agree. Here's the link: http://aspose.com/file-tools

www.coderanch.com/t/244930/java-programmer-SCJP/certification/good-finding-User-Deamon-Threads

3/4

7/14/13

[ A good finding ] - User & Deamon Threads (SCJP forum at JavaRanch)

subject: [ A good finding ] - User & Deamon Threads

Similar Threads JVM and Thread Help Evidence 4 Main Thread dead after main method exits daemon Vs User Thread Daemon Threads How to make a daemon thread
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 14, 2013 08:34:05 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/244930/java-programmer-SCJP/certification/good-finding-User-Deamon-Threads

4/4

Vous aimerez peut-être aussi