Source Code Cross Referenced for GreedyLocksSystemTestApp.java in  » Net » Terracotta » com » tctest » 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 » Terracotta » com.tctest 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice.  All rights reserved.
003:         */
004:        package com.tctest;
005:
006:        import EDU.oswego.cs.dl.util.concurrent.BrokenBarrierException;
007:        import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
008:
009:        import com.tc.object.config.ConfigVisitor;
010:        import com.tc.object.config.DSOClientConfigHelper;
011:        import com.tc.object.config.TransparencyClassSpec;
012:        import com.tc.object.config.spec.CyclicBarrierSpec;
013:        import com.tc.simulator.app.ApplicationConfig;
014:        import com.tc.simulator.listener.ListenerProvider;
015:        import com.tctest.runner.AbstractTransparentApp;
016:
017:        import java.util.ArrayList;
018:        import java.util.List;
019:
020:        public class GreedyLocksSystemTestApp extends AbstractTransparentApp {
021:
022:            public static final int NODE_COUNT = 3;
023:            public static final int EXECUTION_COUNT = 5;
024:            public static final int ITERATION_COUNT = 1;
025:
026:            private List locks = new ArrayList();
027:            private CyclicBarrier barrier = new CyclicBarrier(NODE_COUNT
028:                    * EXECUTION_COUNT);
029:            private String name;
030:            private static int id = 0;
031:            private static int count = 5;
032:
033:            public GreedyLocksSystemTestApp(String appId,
034:                    ApplicationConfig cfg, ListenerProvider listenerProvider) {
035:                super (appId, cfg, listenerProvider);
036:            }
037:
038:            public static void visitL1DSOConfig(ConfigVisitor visitor,
039:                    DSOClientConfigHelper config) {
040:                String testClass = GreedyLocksSystemTestApp.class.getName();
041:                String lockClass = GreedyLocksSystemTestApp.LockObject.class
042:                        .getName();
043:                TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
044:                config.addIncludePattern(lockClass);
045:
046:                String setIDMethodExpression = "* " + testClass + "*.setID(..)";
047:
048:                String readMethodExpression = "* " + lockClass + "*.read(..)";
049:                String writeCountMethodExpression = "* " + lockClass
050:                        + "*.getWriteCount(..)";
051:                String timeoutCountMethodExpression = "* " + lockClass
052:                        + "*.getTimeoutCount(..)";
053:                String notifyCountMethodExpression = "* " + lockClass
054:                        + "*.getNotifyCount(..)";
055:                String writeMethodExpression = "* " + lockClass + "*.write(..)";
056:                String waitNotifyMethodExpression = "* " + lockClass
057:                        + "*.waitAndNotify(..)";
058:
059:                config.addWriteAutolock(setIDMethodExpression);
060:                config.addReadAutolock(readMethodExpression);
061:                config.addReadAutolock(writeCountMethodExpression);
062:                config.addReadAutolock(timeoutCountMethodExpression);
063:                config.addReadAutolock(notifyCountMethodExpression);
064:                config.addWriteAutolock(writeMethodExpression);
065:                config.addWriteAutolock(waitNotifyMethodExpression);
066:                spec.addRoot("locks", "locks");
067:                spec.addRoot("barrier", "barrier");
068:                new CyclicBarrierSpec().visit(visitor, config);
069:            }
070:
071:            public void run() {
072:                name = Thread.currentThread().getName();
073:
074:                setID();
075:
076:                try {
077:                    barrier.barrier();
078:                } catch (BrokenBarrierException e) {
079:                    e.printStackTrace();
080:                } catch (InterruptedException e) {
081:                    e.printStackTrace();
082:                }
083:
084:                for (int i = id - 1; i < locks.size(); i++) {
085:                    LockObject lock = (LockObject) locks.get(i);
086:                    println(" Working on " + lock);
087:                    int read = 0, write = 0;
088:
089:                    // READ loop
090:                    for (int j = 0; j < count; j++) {
091:                        read = lock.read();
092:                    }
093:                    println(lock + ":: After Read loop :: " + read);
094:
095:                    // Write loop
096:                    for (int j = 0; j < count; j++) {
097:                        write = lock.write();
098:                    }
099:                    println(lock + ": After Write loop :: " + write);
100:
101:                    // Wait notify
102:                    for (int j = 0; j < count; j++) {
103:                        lock.waitAndNotify(toString());
104:                    }
105:                    println(lock + ": Timeouts :: " + lock.getTimeoutCount());
106:                    println(lock + ": Notifies :: " + lock.getNotifyCount());
107:
108:                    // READ, WRITE and Wait Notify
109:                    for (int j = 0; j < count; j++) {
110:                        read = lock.read();
111:                        write = lock.write();
112:                        lock.waitAndNotify(toString());
113:                    }
114:                    println(lock + ":: Reads :: " + read);
115:                    println(lock + ":: Writes :: " + write);
116:                    println(lock + ": Timeouts :: " + lock.getTimeoutCount());
117:                    println(lock + ": Notifies :: " + lock.getNotifyCount());
118:                }
119:            }
120:
121:            private void println(String string) {
122:                System.out.println(toString() + string);
123:            }
124:
125:            public String toString() {
126:                return "Client(" + id + ")::" + name + "::";
127:            }
128:
129:            private void setID() {
130:                synchronized (locks) {
131:                    if (id == 0) {
132:                        // first thread
133:                        id = locks.size() + 1;
134:                        locks.add(new LockObject(locks, id));
135:                        // count *= id;
136:                    }
137:                }
138:            }
139:
140:            private static class LockObject {
141:
142:                private List locks;
143:                private int lockID;
144:
145:                int writeCount = 0;
146:                int waitCount = 0;
147:                int timeoutCount = 0;
148:                int notifiedCount = 0;
149:
150:                LockObject(List list, int id) {
151:                    this .locks = list;
152:                    this .lockID = id;
153:                }
154:
155:                synchronized int read() {
156:                    int c = writeCount;
157:                    for (int i = 0; i < locks.size(); i++) {
158:                        LockObject lock = (LockObject) locks.get(i);
159:                        if (lock != this ) {
160:                            c += lock.getWriteCount();
161:                        }
162:                    }
163:                    return c;
164:                }
165:
166:                synchronized void waitAndNotify(String name) {
167:                    waitCount++;
168:                    try {
169:                        long start = System.currentTimeMillis();
170:                        wait(500);
171:                        if ((System.currentTimeMillis() - start) >= 500) {
172:                            timeoutCount++;
173:                        } else {
174:                            notifiedCount++;
175:                        }
176:                    } catch (InterruptedException e) {
177:                        e.printStackTrace();
178:                    }
179:                    waitCount--;
180:                    switch (waitCount % 3) {
181:                    case 0:
182:                        break;
183:                    case 1:
184:                        notify();
185:                        break;
186:                    case 2:
187:                        notifyAll();
188:                    }
189:                }
190:
191:                int getWriteCount() {
192:                    return writeCount;
193:                }
194:
195:                int getTimeoutCount() {
196:                    return timeoutCount;
197:                }
198:
199:                int getNotifyCount() {
200:                    return notifiedCount;
201:                }
202:
203:                synchronized int write() {
204:                    int c = ++writeCount;
205:                    for (int i = 0; i < locks.size(); i++) {
206:                        LockObject lock = (LockObject) locks.get(i);
207:                        if (lock != this ) {
208:                            c += lock.getWriteCount();
209:                        }
210:                    }
211:                    return c;
212:                }
213:
214:                int getID() {
215:                    return lockID;
216:                }
217:
218:                public String toString() {
219:                    return "LockObject(" + getID() + ")";
220:                }
221:            }
222:
223:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.