Show ParallelHello.java syntax highlighted
/*
* Copyright (c) 2006
* Helsinki Institute of Physics
* see LICENSE file for details
*
* HelloWorldMulti.java
*/
import java.io.IOException;
import java.net.InetAddress;
import fi.hip.gb.mobile.AgentUndeployer;
import fi.hip.gb.mobile.MobileAgent;
/**
* A simple example of how to create parallel applications. The target
* server should have AOP enabled.
*
* @author Juho Karppinen
*/
@MobileAgent (singleton=true,discoveryURL="http://opengrid.cern.ch:8080/gb-agent")
public class ParallelHello {
public ParallelHello() {
}
/**
* Creates n parallel HelloWorld threads.
*
* @param param parameter for the hello world
* @param multiply multiply hello messages n times
* @return combined hello world message
*/
public String getHello(final String param, int multiply) throws IOException {
//System.out.println("Hello method '" + param + "' , '" + multiply + "'");
final StringBuffer result = new StringBuffer();
// start processes on the background
Thread[] proc = new Thread[multiply];
for (int i = 0; i < multiply; i++) {
proc[i] = new Thread(new Runnable() {
public void run() {
try {
result.append("\t" + new HelloWorld().getHello(param) + "\n");
} catch (IOException e) {
result.append("\t failed: " + e.getMessage() + "\n");
}
}
});
proc[i].start();
}
// wait until everything is finished
for (int i = 0; i < multiply; i++) {
try {
proc[i].join();
} catch (InterruptedException e) {
throw new IOException("InterruptedException " + e.getMessage());
}
}
return "Hello world X on " + InetAddress.getLocalHost().getHostName()
+ " results to\n " + result;
}
@AgentUndeployer
public void undeploy() {
}
public static void main(String[] args) throws Exception {
ParallelHello hello = new ParallelHello();
System.out.println("returned: " + hello.getHello("hi", 2));
hello.undeploy();
}
}
See more files for this project here