Source Code Cross Referenced for ConcurrentThread.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 j2mex.realtime.MemoryArea;
012:        import j2mex.realtime.RealtimeThread;
013:        import javolution.lang.Reflection;
014:
015:        /**
016:         * <p> This class represents the concurrent executors used by the default 
017:         *     implementation of {@link ConcurrentContext}. Executions
018:         *     are performed in the same memory area and at the same priority
019:         *     as the calling thread.</p>
020:         *
021:         * @author  <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
022:         * @version 5.1, July 1, 2007
023:         */
024:        class ConcurrentThread extends RealtimeThread {
025:
026:            private volatile Runnable _logic;
027:
028:            private MemoryArea _memoryArea;
029:
030:            private int _priority;
031:
032:            private ConcurrentContext.Default _context;
033:
034:            private boolean _terminate;
035:
036:            private String _name;
037:
038:            private Thread _source;
039:
040:            /**
041:             * Default constructor.
042:             */
043:            public ConcurrentThread() {
044:                _name = "ConcurrentThread-" + getCount();
045:                if (SET_NAME != null) {
046:                    SET_NAME.invoke(this , _name);
047:                }
048:                if (SET_DAEMON != null) {
049:                    SET_DAEMON.invoke(this , new Boolean(true));
050:                }
051:            }
052:
053:            private synchronized int getCount() {
054:                return _Count++;
055:            }
056:
057:            private static int _Count;
058:
059:            private static final Reflection.Method SET_NAME = Reflection
060:                    .getMethod("java.lang.Thread.setName(String)");
061:
062:            private static final Reflection.Method SET_DAEMON = Reflection
063:                    .getMethod("java.lang.Thread.setDaemon(boolean)");
064:
065:            /**
066:             * Executes the concurrent logics sequentially.
067:             */
068:            public void run() {
069:                while (true) { // Main loop.
070:                    synchronized (this ) { // Waits for a task.
071:                        try {
072:                            while ((_logic == null) && !_terminate)
073:                                this .wait();
074:                        } catch (InterruptedException e) {
075:                            throw new ConcurrentException(e);
076:                        }
077:                    }
078:                    if (_logic == null)
079:                        break; // Terminates.
080:                    try {
081:                        Thread current = Thread.currentThread();
082:                        if (current.getPriority() != _priority) {
083:                            current.setPriority(_priority);
084:                        }
085:                        _context.started();
086:                        _memoryArea.executeInArea(_logic);
087:                    } catch (Throwable error) {
088:                        _context.error(error);
089:                    } finally {
090:                        _context.completed();
091:                        _logic = null; // Last (ready).
092:                    }
093:                }
094:            }
095:
096:            /**
097:             * Executes the specified logic by this thread if ready.
098:             * 
099:             * @param logic the logic to execute.
100:             * @param context the concurrent context.
101:             */
102:            public boolean execute(Runnable logic,
103:                    ConcurrentContext.Default context) {
104:                if (_logic != null)
105:                    return false; // Shortcut to avoid synchronizing.
106:                synchronized (this ) {
107:                    if (_logic != null)
108:                        return false; // Synchronized check.
109:                    _memoryArea = RealtimeThread.getCurrentMemoryArea();
110:                    _priority = Thread.currentThread().getPriority();
111:                    _context = context;
112:                    _logic = logic; // Must be last.
113:                    this .notify();
114:                    return true;
115:                }
116:            }
117:
118:            // Implements ConcurrentExecutor
119:            public void terminate() {
120:                synchronized (this ) {
121:                    _terminate = true;
122:                    this .notify();
123:                }
124:            }
125:
126:            /**
127:             * Returns the name of this concurrent thread as well as its calling source 
128:             * (in parenthesis).
129:             * 
130:             * @return the string representation of this thread.
131:             */
132:            public String toString() {
133:                return _name + "(" + _source + ")";
134:            }
135:
136:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.