Source Code Cross Referenced for HeapContext.java in  » Science » javolution-5.2 » javolution » context » 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 » Science » javolution 5.2 » javolution.context 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
003:         * Copyright (C) 2006 - Javolution (http://javolution.org/)
004:         * All rights reserved.
005:         * 
006:         * Permission to use, copy, modify, and distribute this software is
007:         * freely granted, provided that this notice is preserved.
008:         */
009:        package javolution.context;
010:
011:        import j2me.lang.ThreadLocal;
012:        import javolution.util.FastMap;
013:        import javolution.util.FastTable;
014:
015:        /**
016:         * <p> This class represents the default allocator context. Allocations are 
017:         *     performed using the <code>new</code> keyword and explicit object 
018:         *     {@link ObjectFactory#recycle(Object) recycling} is supported:[code]
019:         *         char[] buffer = ArrayFactory.CHARS_FACTORY.array(4098); // Possibly recycled.
020:         *         while (reader.read(buffer) > 0) { ... }
021:         *         ArrayFactory.CHARS_FACTORY.recycle(buffer); // Explicit recycling.
022:         *     [/code]</p>
023:         *       
024:         * @author  <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
025:         * @version 5.2, August 19, 2007
026:         */
027:        public class HeapContext extends AllocatorContext {
028:
029:            /**
030:             * Holds the factory to allocator mapping (per thread).
031:             */
032:            private static final ThreadLocal FACTORY_TO_ALLOCATOR = new ThreadLocal() {
033:                protected Object initialValue() {
034:                    return new FastMap();
035:                }
036:            };
037:
038:            /**
039:             * Holds the allocators which have been activated (per thread).
040:             */
041:            private static final ThreadLocal ACTIVE_ALLOCATORS = new ThreadLocal() {
042:                protected Object initialValue() {
043:                    return new FastTable();
044:                }
045:            };
046:
047:            /**
048:             * Enters a heap context.
049:             * 
050:             * @return the heap context entered.
051:             */
052:            public static HeapContext enter() {
053:                return (HeapContext) Context.enter(CLASS);
054:            }
055:
056:            private static final Class CLASS = new HeapContext().getClass();
057:
058:            /**
059:             * Exits the current heap context.
060:             * 
061:             * @return the heap context being exited.
062:             * @throws ClassCastException if the context is not a heap context.
063:             */
064:            public static/*HeapContext*/Context exit() {
065:                return (HeapContext) Context.exit();
066:            }
067:
068:            /**
069:             * Default constructor.
070:             */
071:            public HeapContext() {
072:            }
073:
074:            // Overrides.
075:            protected void deactivate() {
076:                FastTable allocators = (FastTable) ACTIVE_ALLOCATORS.get();
077:                for (int i = 0, n = allocators.size(); i < n;) {
078:                    ((Allocator) allocators.get(i++)).user = null;
079:                }
080:                allocators.clear();
081:            }
082:
083:            // Overrides.
084:            protected Allocator getAllocator(ObjectFactory factory) {
085:                final FastMap factoryToAllocator = (FastMap) FACTORY_TO_ALLOCATOR
086:                        .get();
087:                HeapAllocator allocator = (HeapAllocator) factoryToAllocator
088:                        .get(factory);
089:                if (allocator == null) {
090:                    allocator = new HeapAllocator(factory);
091:                    factoryToAllocator.put(factory, allocator);
092:                }
093:                if (allocator.user == null) { // Activate.
094:                    allocator.user = Thread.currentThread();
095:                    FastTable activeAllocators = (FastTable) ACTIVE_ALLOCATORS
096:                            .get();
097:                    activeAllocators.add(allocator);
098:                }
099:                return allocator;
100:            }
101:
102:            // Overrides.
103:            protected void enterAction() {
104:                getOuter().getAllocatorContext().deactivate();
105:            }
106:
107:            // Overrides.
108:            protected void exitAction() {
109:                this .deactivate();
110:            }
111:
112:            // Holds heap allocator implementation.
113:            private static final class HeapAllocator extends Allocator {
114:
115:                private final ObjectFactory _factory;
116:
117:                public HeapAllocator(ObjectFactory factory) {
118:                    _factory = factory;
119:                }
120:
121:                protected Object allocate() {
122:                    return _factory.create();
123:                }
124:
125:                protected void recycle(Object object) {
126:                    if (_factory.doCleanup()) {
127:                        _factory.cleanup(object);
128:                    }
129:                    if (queueSize >= queue.length) {
130:                        resize();
131:                    }
132:                    queue[queueSize++] = object;
133:                }
134:
135:                public String toString() {
136:                    return "Heap allocator for " + _factory.getClass();
137:                }
138:            }
139:
140:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.