Source Code Cross Referenced for DaemonThread.java in  » Database-ORM » MMBase » org » mmbase » core » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » MMBase » org.mmbase.core.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:
003:        This software is OSI Certified Open Source Software.
004:        OSI Certified is a certification mark of the Open Source Initiative.
005:
006:        The license (Mozilla version 1.0) can be read at the MMBase site.
007:        See http://www.MMBase.org/license
008:
009:         */
010:        package org.mmbase.core.util;
011:
012:        import org.mmbase.module.core.MMBaseContext;
013:        import org.mmbase.util.logging.*;
014:
015:        /**
016:         * Defines a daemon thread that runs in the threadgroup belonging to this MMBase context.
017:         * @since MMBase-1.8
018:         * @author Pierre van Rooden
019:         * @version $Id: DaemonThread.java,v 1.2 2005/12/10 11:45:02 michiel Exp $
020:         */
021:        public class DaemonThread extends Thread implements  DaemonTask {
022:
023:            /**
024:             * Default sleep period for a daemon thread (one minute).
025:             */
026:            public static final int DEFAULT_SLEEP_PERIOD = 60000;
027:
028:            private static final Logger log = Logging
029:                    .getLoggerInstance(DaemonThread.class);
030:
031:            /**
032:             * The threads sleep period.
033:             * This period is used when a Daemonthread runs on its own (that is, without an assigned task)
034:             * When a DaemonThread is assigned a task, it uses the sleep period of that task.
035:             */
036:            protected int sleepPeriod = DEFAULT_SLEEP_PERIOD;
037:
038:            private Runnable target = null;
039:            private DaemonTask task = null;
040:            private boolean running = false;
041:
042:            /**
043:             * Create a MMBase daemon thread (associated with this MMBase's threadgroup).
044:             */
045:            public DaemonThread() {
046:                this ((Runnable) null, (String) null);
047:            }
048:
049:            /**
050:             * Create a MMBase daemon thread (associated with this MMBase's threadgroup).
051:             * @param name the name of the thread
052:             */
053:            public DaemonThread(String name) {
054:                this ((Runnable) null, name);
055:            }
056:
057:            /**
058:             * Create a MMBase daemon thread (associated with this MMBase's threadgroup).
059:             * @param target the target thread
060:             * @param name the name of the thread
061:             */
062:            public DaemonThread(Runnable target, String name) {
063:                super (MMBaseContext.getThreadGroup(), target, name);
064:                this .target = target;
065:                setDaemon(true);
066:            }
067:
068:            /**
069:             * Sets the task this thread should run when started.
070:             * @param task the task to run
071:             */
072:            public void setTask(DaemonTask task) {
073:                this .task = task;
074:            }
075:
076:            /**
077:             * Returns the task this thread runs when started.
078:             */
079:            public DaemonTask getTask() {
080:                return task;
081:            }
082:
083:            public int getSleepPeriod() {
084:                if (task != null) {
085:                    return task.getSleepPeriod();
086:                } else {
087:                    return sleepPeriod;
088:                }
089:            }
090:
091:            public void start() {
092:                running = true;
093:                log.service("Starting " + getName());
094:                super .start();
095:            }
096:
097:            public void interrupt() {
098:                running = false;
099:                super .interrupt();
100:            }
101:
102:            public boolean isRunning() {
103:                return running;
104:            }
105:
106:            public void executeTask() {
107:                if (task != null) {
108:                    task.executeTask();
109:                } else {
110:                    throw new UnsupportedOperationException(
111:                            "No execute task defined");
112:                }
113:            }
114:
115:            /**
116:             * Default behavior (when no target is specified) is to run continuously until interrupted.
117:             */
118:            public void run() {
119:                if (target != null) {
120:                    target.run();
121:                } else {
122:                    while (isRunning()) {
123:                        try {
124:                            Thread.sleep(getSleepPeriod());
125:                            executeTask();
126:                        } catch (InterruptedException e) {
127:                            log.debug(Thread.currentThread().getName()
128:                                    + " was interrupted.");
129:                        }
130:                    }
131:                }
132:            }
133:
134:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.