Source Code Cross Referenced for SetOnceFlagTest.java in  » Net » Terracotta » com » tc » util » concurrent » 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.tc.util.concurrent 
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.tc.util.concurrent;
005:
006:        import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean;
007:
008:        import java.util.Random;
009:
010:        import junit.framework.TestCase;
011:
012:        /**
013:         * Test cases for SetOnceFlag
014:         * 
015:         * @author teck
016:         */
017:        public class SetOnceFlagTest extends TestCase {
018:
019:            public void testRace() throws InterruptedException {
020:                final Random random = new Random();
021:
022:                for (int i = 0; i < 50; i++) {
023:                    final SetOnceFlag flag = new SetOnceFlag();
024:                    final SynchronizedBoolean thread1 = new SynchronizedBoolean(
025:                            false);
026:                    final SynchronizedBoolean thread2 = new SynchronizedBoolean(
027:                            false);
028:
029:                    Runnable r1 = new Runnable() {
030:                        public void run() {
031:                            try {
032:                                Thread.sleep(random.nextInt(50));
033:                            } catch (InterruptedException e) {
034:                                fail();
035:                            }
036:
037:                            try {
038:                                flag.set();
039:                                thread1.set(true); // I win!
040:                            } catch (IllegalStateException iae) {
041:                                // I didn't win ;-(
042:                            }
043:                        }
044:                    };
045:
046:                    Runnable r2 = new Runnable() {
047:                        public void run() {
048:                            try {
049:                                try {
050:                                    Thread.sleep(random.nextInt(50));
051:                                } catch (InterruptedException e) {
052:                                    fail();
053:                                }
054:
055:                                flag.set();
056:                                thread2.set(true); // I win!
057:                            } catch (IllegalStateException iae) {
058:                                // I didn't win ;-(
059:                            }
060:                        }
061:                    };
062:
063:                    Thread t1 = new Thread(r1);
064:                    Thread t2 = new Thread(r2);
065:                    t1.start();
066:                    t2.start();
067:                    t1.join();
068:                    t2.join();
069:
070:                    System.out.println("The winner is thread "
071:                            + (thread1.get() ? "1" : "2"));
072:
073:                    assertTrue(thread1.get() ^ thread2.get());
074:                }
075:            }
076:
077:            public void testAttemptSet() {
078:                SetOnceFlag flag = new SetOnceFlag();
079:
080:                flag.set();
081:
082:                assertFalse(flag.attemptSet());
083:            }
084:
085:            public void testMultiSet() {
086:                SetOnceFlag flag = new SetOnceFlag();
087:
088:                // set it once
089:                flag.set();
090:
091:                // try setting it many more times
092:                for (int i = 0; i < 100; i++) {
093:                    try {
094:                        flag.set();
095:                        fail();
096:                    } catch (IllegalStateException iae) {
097:                        // expected
098:                    }
099:                }
100:            }
101:
102:            public void testMultiRead() {
103:                SetOnceFlag flag = new SetOnceFlag();
104:
105:                // set it once
106:                flag.set();
107:
108:                // try reading it many times
109:                for (int i = 0; i < 100; i++) {
110:                    assertTrue(flag.isSet());
111:                }
112:            }
113:
114:            public void testInitSet() {
115:                SetOnceFlag flag = new SetOnceFlag(true);
116:
117:                try {
118:                    flag.set();
119:                    fail();
120:                } catch (IllegalStateException iae) {
121:                    // expected
122:                }
123:            }
124:
125:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.