Source Code Cross Referenced for ThreadPoolExecutor.java in  » Workflow-Engines » Dalma » dalma » helpers » 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 » Workflow Engines » Dalma » dalma.helpers 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package dalma.helpers;
002:
003:        import dalma.Engine;
004:        import dalma.Executor;
005:
006:        import java.util.ArrayList;
007:        import java.util.Collection;
008:        import java.util.LinkedList;
009:        import java.util.List;
010:
011:        /**
012:         * {@link Executor} implemented as a thread pool.
013:         *
014:         * @author Kohsuke Kawaguchi
015:         */
016:        public class ThreadPoolExecutor implements  Executor {
017:
018:            /**
019:             * Queue of conversations that can be run.
020:             * Needs to be synchronized before use.
021:             */
022:            private final List<Runnable> jobQueue = new LinkedList<Runnable>();
023:
024:            private final Collection<WorkerThread> threads = new ArrayList<WorkerThread>();
025:
026:            /**
027:             * This object signals when all the threads terminate.
028:             */
029:            private final Object terminationSignal = new Object();
030:
031:            /**
032:             * True to make worker threads daemon thread.
033:             */
034:            private final boolean daemon;
035:
036:            /**
037:             * Set to true if we start stopping.
038:             *
039:             * All the worker threads are interrupted, but applications
040:             * often eat interruption, so it's better to have this too, to be safe.
041:             */
042:            private boolean isStopping;
043:
044:            /**
045:             * Creates a new thread pool executor.
046:             *
047:             * @param nThreads
048:             *      number of worker threads to create.
049:             * @param daemon
050:             *      true to make worker threads daemon threads.
051:             *      daemon threads allows the VM to shut down as soon as
052:             *      the application thread exits. Otherwise, you have to
053:             *      call {@link Engine#stop()} before your main thread exits,
054:             *      or else the JVM will run forever.
055:             */
056:            public ThreadPoolExecutor(int nThreads, boolean daemon) {
057:                this .daemon = daemon;
058:                synchronized (threads) {
059:                    for (int i = 0; i < nThreads; i++) {
060:                        WorkerThread thread = new WorkerThread();
061:                        thread.start();
062:                        threads.add(thread);
063:                    }
064:                }
065:            }
066:
067:            public ThreadPoolExecutor(int nThreads) {
068:                this (nThreads, false);
069:            }
070:
071:            protected void finalize() throws Throwable {
072:                super .finalize();
073:                sendKillSignal();
074:                // let them die, but don't wait
075:            }
076:
077:            private void sendKillSignal() {
078:                synchronized (threads) {
079:                    isStopping = true;
080:                    for (WorkerThread t : threads)
081:                        t.interrupt();
082:                }
083:            }
084:
085:            public void execute(Runnable command) {
086:                synchronized (jobQueue) {
087:                    jobQueue.add(command);
088:                    jobQueue.notify();
089:                }
090:            }
091:
092:            public void stop(long timeout) throws InterruptedException {
093:                synchronized (terminationSignal) {
094:                    sendKillSignal();
095:                    if (timeout == -1)
096:                        terminationSignal.wait();
097:                    else
098:                        terminationSignal.wait(timeout);
099:                }
100:            }
101:
102:            private final class WorkerThread extends Thread {
103:                public WorkerThread() {
104:                    super ("Dalma engine worker thread");
105:                    setDaemon(daemon);
106:                }
107:
108:                public void run() {
109:                    try {
110:                        while (!isStopping) {
111:                            Runnable job;
112:                            synchronized (jobQueue) {
113:                                while (jobQueue.isEmpty())
114:                                    jobQueue.wait();
115:                                job = jobQueue.remove(0);
116:                            }
117:
118:                            job.run();
119:                        }
120:                    } catch (InterruptedException e) {
121:                        // treat this as a signal to die
122:                    } finally {
123:                        synchronized (threads) {
124:                            threads.remove(this);
125:                            if (threads.isEmpty()) {
126:                                synchronized (terminationSignal) {
127:                                    terminationSignal.notifyAll();
128:                                }
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.