Source Code Cross Referenced for ClientThread.java in  » Net » QuickServer » org » quickserver » util » pool » thread » 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 » Net » QuickServer » org.quickserver.util.pool.thread 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * This file is part of the QuickServer library 
003:         * Copyright (C) QuickServer.org
004:         *
005:         * Use, modification, copying and distribution of this software is subject to
006:         * the terms and conditions of the GNU Lesser General Public License. 
007:         * You should have received a copy of the GNU LGP License along with this 
008:         * library; if not, you can download a copy from <http://www.quickserver.org/>.
009:         *
010:         * For questions, suggestions, bug-reports, enhancement-requests etc.
011:         * visit http://www.quickserver.org
012:         *
013:         */
014:
015:        package org.quickserver.util.pool.thread;
016:
017:        import java.util.logging.*;
018:        import java.util.*;
019:        import org.quickserver.util.MyString;
020:        import org.quickserver.net.server.ClientHandler;
021:        import org.quickserver.net.server.ClientEvent;
022:
023:        /**
024:         * This is the worker thread used to handle clients using 
025:         * {@link org.quickserver.net.server.ClientHandler}
026:         * @author Akshathkumar Shetty
027:         * @since 1.3
028:         */
029:        public class ClientThread extends Thread {
030:            private static Logger logger = Logger.getLogger(ClientThread.class
031:                    .getName());
032:            private static Map idMap = new HashMap();
033:
034:            private String name = "<ClientThread-Pool#";
035:            private ClientPool pool;
036:            private Runnable client;
037:            private int id;
038:            private boolean ready = false;
039:
040:            /**
041:             * Holds the current Thread state. <code><pre>
042:            	U = Unknown
043:            	S = Started
044:            	R - Running a client
045:            	I = Idle
046:            	L = Looking for client
047:            	P = Was sent back to pool
048:            	W = Waiting in pool		
049:            	N = Was notified, Looking for client
050:            	D = Dead
051:            	</pre></code>
052:             */
053:            protected volatile char state = 'U';
054:
055:            public boolean isReady() {
056:                return ready;
057:            }
058:
059:            public void clean() {
060:                client = null;
061:            }
062:
063:            public ClientThread(ClientPool pool) {
064:                this (pool, -1);
065:            }
066:
067:            static class InstanceId {
068:                private int id = 0;
069:
070:                public int getNextId() {
071:                    return ++id;
072:                }
073:            };
074:
075:            private static int getNewId(int instanceCount) {
076:                InstanceId instanceId = (InstanceId) idMap.get(""
077:                        + instanceCount);
078:                if (instanceId == null) {
079:                    instanceId = new InstanceId();
080:                    idMap.put("" + instanceCount, instanceId);
081:                }
082:                return instanceId.getNextId();
083:            }
084:
085:            public ClientThread(ClientPool pool, int instanceCount) {
086:                id = getNewId(instanceCount);
087:                name = name + instanceCount + "-ID:" + id + ">";
088:                this .pool = pool;
089:                setName(name);
090:            }
091:
092:            public int getInstanceId() {
093:                return id;
094:            }
095:
096:            private void executeClient() {
097:                boolean niowriteFlag = false;
098:                state = 'R';
099:
100:                if (ClientHandler.class.isInstance(client)) {
101:                    niowriteFlag = ((ClientHandler) client)
102:                            .isClientEventNext(ClientEvent.WRITE);
103:                    if (niowriteFlag) {
104:                        pool.nioWriteStart();
105:                    }
106:                } else {
107:                    niowriteFlag = false;
108:                }
109:
110:                try {
111:                    client.run();
112:                } catch (RuntimeException e) {
113:                    logger.warning("RuntimeException @ thread run() : "
114:                            + getName() + ": " + MyString.getStackTrace(e));
115:                } finally {
116:                    if (niowriteFlag) {
117:                        pool.nioWriteEnd();
118:                    }
119:                }
120:                state = 'I';
121:            }
122:
123:            public void run() {
124:                state = 'S';
125:
126:                if (pool.isClientAvailable() == true) {
127:                    ready = true;
128:                    synchronized (pool) {
129:                        pool.notify();
130:                    }
131:                }
132:
133:                boolean returnToPool = false;
134:                while (true) {
135:                    if (ready) {
136:                        state = 'L';
137:                        client = pool.getClient();
138:                        if (client == null) {
139:                            logger
140:                                    .fine("ClientPool returned a null client! Other Thread must have taken my client.. Ok");
141:                        } else {
142:                            executeClient();
143:                            logger.finest("Client returned the thread: "
144:                                    + getName());
145:                            client = null;
146:                            if (pool == null) {
147:                                logger
148:                                        .fine("Could not returning client thread "
149:                                                + getName()
150:                                                + ", pool was null!");
151:                                state = 'D';
152:                                break;
153:                            }
154:                        }
155:
156:                        if (pool.isClientAvailable() == true) {
157:                            state = 'L';
158:                            continue;
159:                        }
160:
161:                        returnToPool = true;
162:                    } //end if ready
163:
164:                    synchronized (this ) {
165:                        if (ready == false)
166:                            ready = true;
167:
168:                        if (returnToPool) {
169:                            logger.finest("Returning client thread to pool: "
170:                                    + getName());
171:                            pool.returnObject(ClientThread.this );
172:                            returnToPool = false;
173:                            state = 'P';
174:                        }
175:
176:                        try {
177:                            state = 'W';
178:                            wait();
179:                            state = 'N';
180:                        } catch (InterruptedException e) {
181:                            logger.finest("Closing thread "
182:                                    + Thread.currentThread().getName()
183:                                    + " since interrupted.");
184:                            state = 'D';
185:                            break;
186:                        }
187:                    }
188:                }//end while
189:            }
190:
191:            /**
192:             * Returns the {@link org.quickserver.net.server.ClientHandler} beeing 
193:             * run by the ClientThread.
194:             * @since 1.3.1
195:             */
196:            public Runnable getThread() {
197:                return client;
198:            }
199:
200:            /**
201:             * [ThreadInPool[<Instance Count>]:<id>] - <state> - Client {ClientHandler:...}
202:             * @since 1.4.1
203:             */
204:            public String toString() {
205:                return super .toString() + " - " + state + " - Client " + client;
206:            }
207:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.