Here is a "HelloWorld" program in X10:
v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} p\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} v\:textbox {display:none;}
classHelloWholeWorld {
public static defmain(s:Array[String]):void {
Console.OUT.println("Hello, world!");
}
}
When this program is run, the body of the main method is executed in an activity at place 0 with the command parameters supplied as a 1-dimensional array of strings. The body contains a single statement which is executed, causing "Hello, World" to be printed out, and the program terminates.
What if we wanted to print out a string from thr command line from every place in the execution? Here is what you would do:
v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} p\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} v\:textbox {display:none;}
classHelloWholeWorld {
public static defmain(s:Array[String]):void {
finish
for(pinPlace.places())
async
at(p)
Console.OUT.println("(At " + p + ") "+ s(0));
}
}
From place 0, for every place p in the set of places you would launch an async thart immediately shifts to place p, and prints out the given string. Note that the println references a variable s that was originally declared at place 0 (as the parameter to the method). The X10 system takes care of serializing into a buffer the objects referred to in the body of the async at(p) ... transmitting the buffer to all places and reconstituting a local copy of the object in each place. The println statement then accesses the 0'th element of the copy at this place.
Since you want the program to terminate only when all places have finished their work, you enclose the for loop in a finish.