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