Вы находитесь на странице: 1из 3

7/14/13

How to make a daemon thread (Beginning Java forum at JavaRanch)

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Java Beginning Java

Author
santhosh.R gowda Ranch Hand Joined: Apr 06, 2009 Posts: 296

How to make a daemon thread


posted 10/21/2009 5:21:57 PM

Hi All I want to write a code to make a daemon thread. That thread should be running in the background every time as and when the system started(Ex-jvm). And also this should execute particular method in the midnight. regards santosh
C reativity is nothing but Breaking Rules

Rahul P Kumar Ranch Hand Joined: Sep 26, 2009 Posts: 188

posted 10/21/2009 5:23:43 PM

probably you need scheduler with this thread. making a thread daemon is easy. look into web.

Ulf Dittmer Marshal Joined: Mar 22, 2005 Posts: 35716

posted 10/21/2009 5:42:23 PM

Check out the java.util.Timer and TimerTask classes - this sounds like a perfect application for them (and they can be made to run as background threads).

10

Android apps ImageJ plugins Java web charts


1/3

www.coderanch.com/t/467460/java/java/daemon-thread

7/14/13

How to make a daemon thread (Beginning Java forum at JavaRanch)

Vadivel Murugesan Greenhorn Joined: Jul 19, 2008 Posts: 11

posted 10/21/2009 5:48:23 PM

In Java, any thread can be a Daemon thread. Daemon threads are like a service providers for other threads or objects running in the same process as the daemon thread. Daemon threads are used for background supporting tasks and are only needed while normal threads are executing. If normal threads are not running and remaining threads are daemon threads then the interpreter exits. setDaemon(true/false) /b> This method is used to specify that a thread is daemon thread. public boolean isDaemon() This method is used to determine the thread is daemon thread or not. The following program demonstrates the Daemon Thread:
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 . 3 4 . 3 5 . 3 6 .

p u b l i cc 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 ( ){ S y s t e m . o u t . p r i n t l n ( " E n t e r i n gr u nm e t h o d " ) ; t r y{ S y s t e m . o u t . p r i n t l n ( " I nr u nM e t h o d :c u r r e n t T h r e a d ( ) i s " +T h r e a d . c u r r e n t T h r e a d ( ) ) ; w h i l e( t r u e ){ t r y{ T h r e a d . s l e e p ( 5 0 0 ) ; }c a t c h( I n t e r r u p t e d E x c e p t i o nx ){ } S y s t e m . o u t . p r i n t l n ( " I nr u nm e t h o d :w o k eu pa g a i n " ) ; } }f i n a l l y{ S y s t e m . o u t . p r i n t l n ( " L e a v i n gr u nM e t h o d " ) ; } } 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 ( " E n t e r i n gm a i nM e t h o 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 ( ) ; t r y{ T h r e a d . s l e e p ( 3 0 0 0 ) ; }c a t c h( I n t e r r u p t e d E x c e p t i o nx ){ } S y s t e m . o u t . p r i n t l n ( " L e a v i n gm a i nm e t h o d " ) ; } }
2/3

www.coderanch.com/t/467460/java/java/daemon-thread

7/14/13

How to make a daemon thread (Beginning Java forum at JavaRanch)

Levi<br />SC JP 1.4

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

subject: How to make a daemon thread

Similar Threads regarding daemon thread Spawning threads from Servlets? daemon Thread Java Thread need clarification on threads
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:37 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/467460/java/java/daemon-thread

3/3

Вам также может понравиться