Java Reference
In-Depth Information
import java.math.BigDecimal;
public class Pi implements Task {
private int decimals;
/** Calculate Pi to a given number of decimal places */
public Pi(int decimals) {
this.decimals = decimals;
}
public Object run() {
BigDecimal res = computePi();
return res;
}
BigDecimal computePi() {
// ...
}
}
The
Pi
class implements the
Task
interface with a
run
method that re-
turns a
java.math.BigDecimal
object containing the computed result. You
then put the compiled
Pi
class someplace where it can be downloaded
from a
URL
, just as an applet class would be. The
URL
from which to
download the code will be set as a property for your client.
When you invoke the compute server's
compute
method, passing a
Pi
ob-
ject, the server will need the
Pi
class. It will look at the
URL
that has
been implicitly passed with the request, download the class, and run it
in a secure sandbox. It will then invoke the
Pi
object's
run
method and
return the result.
This
ComputeServer
example leverages the Java virtual machine's homo-
geneous computing model, in which all code means the same thing on
all platforms, and its security model, in which downloaded code can be
run securely.