1 4 package demo.coordination; 5 6 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier; 7 import java.text.DateFormat ; 8 import java.util.Date ; 9 10 14 public class Main 15 { 16 private static String 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 private static String 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 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 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 54 public Main(int expectedParticipants) 55 { 56 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 72 public void run() 73 { 74 try 75 { 76 System.out.println(text1.replaceFirst("@TOKEN@", Integer.toString(expectedParticipants))); 78 enterBarrier.barrier(); 79 80 String currentDateAndTime = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(new Date ()); 82 System.out.println(text2.replaceFirst("@TOKEN@", this + Integer.toString(expectedParticipants)).replaceFirst("@TOKEN@", currentDateAndTime)); 83 84 exitBarrier.barrier(); 86 System.out.println(text3); 87 } 88 catch(InterruptedException ie) 89 { 90 ie.printStackTrace(); 91 } 92 } 93 94 public static final void main(String [] args) 95 throws Exception 96 { 97 System.out.println(text0); 98 99 int expectedParticipants = 0; 100 try { expectedParticipants = Integer.parseInt(args[0]); } 101 catch (Exception e) { } 102 103 (new Main(expectedParticipants)).run(); 104 } 105 } 106 | Popular Tags |