KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > coordination > Main


1 /*
2 @COPYRIGHT@
3 */

4 package demo.coordination;
5
6 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
7 import java.text.DateFormat JavaDoc;
8 import java.util.Date JavaDoc;
9
10 /**
11 * Sample to demonstrate how to instrument things like CyclicBarrier
12 * and use them as a distributed mechanism.
13 */

14 public class Main
15 {
16    // banner text displayed on startup
17
private static String JavaDoc text0 =
18       "\n" +
19       "JVM Coordination\n" +
20       "\n" +
21       "This sample application show how to coordinate threads in a multi-VM\n" +
22       "environment using the same patterns one would use in a multi-threaded\n" +
23       "single-VM environment.\n";
24    
25    // these are the text messages we use to display the state of the application
26
private static String JavaDoc text1 =
27       "Application started; I expect a total of @TOKEN@ VMs that will be participating.\n" +
28       "At this point the application is waiting for the other pariticipants (or VMs) to startup.\n" +
29       "When all of the participants are available, it will perform its task and exit.\n\n" +
30       "Notice that all the other participants also come into a wait state just like the first VM that\n" +
31       "you launched; they will only proceed as soon as the number of VMs that you have launched\n" +
32       "matches the number of participants it expects.\n\n" +
33       "Waiting for all other VMs to join...\n";
34    private static String JavaDoc text2 =
35       "I am node: @TOKEN@\n" +
36       "The number of VMs that I expect to participate has launched.\n" +
37       "I will now perform my task by printing today's date and current time:\n\n" +
38       "Here it is:\n@TOKEN@\n\n" +
39       "I have completed my task." +
40       "I am now waiting for all the other VMs finish their task...\n";
41    private static String JavaDoc text3 =
42       "All of the participating VMs have completed their task.\n" +
43       "I am stopping now.";
44
45    private int expectedParticipants;
46    private CyclicBarrier enterBarrier;
47    private CyclicBarrier exitBarrier;
48    private static int MINIMUM_EXPECTED_PARTICIPANTS = 2;
49
50    /**
51     * Create an instance, setting the number of VMs expected to
52     * participate in the demo.
53     */

54    public Main(int expectedParticipants)
55    {
56       // enforce minimum number of participants
57
if (expectedParticipants < MINIMUM_EXPECTED_PARTICIPANTS)
58       {
59          expectedParticipants = MINIMUM_EXPECTED_PARTICIPANTS;
60          System.out.println("(You did not pass an argument, I'm assuming " + expectedParticipants + " VMs will be participating)\n");
61       }
62       this.expectedParticipants = expectedParticipants;
63       this.enterBarrier = new CyclicBarrier(expectedParticipants);
64       this.exitBarrier = new CyclicBarrier(expectedParticipants);
65    }
66
67    /**
68     * Start up multiple threads and wait. Once all the theads have
69     * started, execute some code. When all threads have finished
70     * executing the code, coordinate the shutdown of the participants.
71     */

72    public void run()
73    {
74       try
75       {
76          // wait for all participants before performing tasks
77
System.out.println(text1.replaceFirst("@TOKEN@", Integer.toString(expectedParticipants)));
78          enterBarrier.barrier();
79          
80          // perform task once all of the expected participants is present
81
String JavaDoc currentDateAndTime = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(new Date JavaDoc());
82          System.out.println(text2.replaceFirst("@TOKEN@", this + Integer.toString(expectedParticipants)).replaceFirst("@TOKEN@", currentDateAndTime));
83
84          // wait for all participants to complete their task before exiting
85
exitBarrier.barrier();
86          System.out.println(text3);
87       }
88       catch(InterruptedException JavaDoc ie)
89       {
90          ie.printStackTrace();
91       }
92    }
93
94    public static final void main(String JavaDoc[] args)
95       throws Exception JavaDoc
96    {
97       System.out.println(text0);
98
99       int expectedParticipants = 0;
100       try { expectedParticipants = Integer.parseInt(args[0]); }
101       catch (Exception JavaDoc e) { }
102
103       (new Main(expectedParticipants)).run();
104    }
105 }
106
Popular Tags