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: }
|