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


001:        /*
002:        @COPYRIGHT@
003:         */
004:        package demo.coordination;
005:
006:        import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
007:        import java.text.DateFormat;
008:        import java.util.Date;
009:
010:        /**
011:         * Sample to demonstrate how to instrument things like CyclicBarrier
012:         * and use them as a distributed mechanism.
013:         */
014:        public class Main {
015:            // banner text displayed on startup
016:            private static String text0 = "\n"
017:                    + "JVM Coordination\n"
018:                    + "\n"
019:                    + "This sample application show how to coordinate threads in a multi-VM\n"
020:                    + "environment using the same patterns one would use in a multi-threaded\n"
021:                    + "single-VM environment.\n";
022:
023:            // these are the text messages we use to display the state of the application 
024:            private static String text1 = "Application started; I expect a total of @TOKEN@ VMs that will be participating.\n"
025:                    + "At this point the application is waiting for the other pariticipants (or VMs) to startup.\n"
026:                    + "When all of the participants are available, it will perform its task and exit.\n\n"
027:                    + "Notice that all the other participants also come into a wait state just like the first VM that\n"
028:                    + "you launched; they will only proceed as soon as the number of VMs that you have launched\n"
029:                    + "matches the number of participants it expects.\n\n"
030:                    + "Waiting for all other VMs to join...\n";
031:            private static String text2 = "I am node: @TOKEN@\n"
032:                    + "The number of VMs that I expect to participate has launched.\n"
033:                    + "I will now perform my task by printing today's date and current time:\n\n"
034:                    + "Here it is:\n@TOKEN@\n\n"
035:                    + "I have completed my task."
036:                    + "I am now waiting for all the other VMs finish their task...\n";
037:            private static String text3 = "All of the participating VMs have completed their task.\n"
038:                    + "I am stopping now.";
039:
040:            private int expectedParticipants;
041:            private CyclicBarrier enterBarrier;
042:            private CyclicBarrier exitBarrier;
043:            private static int MINIMUM_EXPECTED_PARTICIPANTS = 2;
044:
045:            /**
046:             * Create an instance, setting the number of VMs expected to 
047:             * participate in the demo.
048:             */
049:            public Main(int expectedParticipants) {
050:                // enforce minimum number of participants
051:                if (expectedParticipants < MINIMUM_EXPECTED_PARTICIPANTS) {
052:                    expectedParticipants = MINIMUM_EXPECTED_PARTICIPANTS;
053:                    System.out
054:                            .println("(You did not pass an argument, I'm assuming "
055:                                    + expectedParticipants
056:                                    + " VMs will be participating)\n");
057:                }
058:                this .expectedParticipants = expectedParticipants;
059:                this .enterBarrier = new CyclicBarrier(expectedParticipants);
060:                this .exitBarrier = new CyclicBarrier(expectedParticipants);
061:            }
062:
063:            /**
064:             * Start up multiple threads and wait.  Once all the theads have
065:             * started, execute some code.  When all threads have finished
066:             * executing the code, coordinate the shutdown of the participants.
067:             */
068:            public void run() {
069:                try {
070:                    // wait for all participants before performing tasks
071:                    System.out.println(text1.replaceFirst("@TOKEN@", Integer
072:                            .toString(expectedParticipants)));
073:                    enterBarrier.barrier();
074:
075:                    // perform task once all of the expected participants is present
076:                    String currentDateAndTime = DateFormat.getDateTimeInstance(
077:                            DateFormat.SHORT, DateFormat.SHORT).format(
078:                            new Date());
079:                    System.out.println(text2.replaceFirst("@TOKEN@",
080:                            this  + Integer.toString(expectedParticipants))
081:                            .replaceFirst("@TOKEN@", currentDateAndTime));
082:
083:                    // wait for all participants to complete their task before exiting
084:                    exitBarrier.barrier();
085:                    System.out.println(text3);
086:                } catch (InterruptedException ie) {
087:                    ie.printStackTrace();
088:                }
089:            }
090:
091:            public static final void main(String[] args) throws Exception {
092:                System.out.println(text0);
093:
094:                int expectedParticipants = 0;
095:                try {
096:                    expectedParticipants = Integer.parseInt(args[0]);
097:                } catch (Exception e) {
098:                }
099:
100:                (new Main(expectedParticipants)).run();
101:            }
102:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.