import io.ray.api.ActorHandle; import io.ray.api.ObjectRef; import io.ray.api.Ray; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors;
public class RayDemo {
public static int square(int x) { return x * x; }
public static class Counter {
private int value = 0;
public void increment() { this.value += 1; }
public int read() { return this.value; } }
public static void main(String[] args) { // Intialize Ray runtime. Ray.init(); { List<ObjectRef<Integer>> objectRefList = new ArrayList<>(); // Invoke the <code>square</code> method 4 times remotely as Ray tasks. // The tasks will run in parallel in the background. for (int i = 0; i < 4; i++) { objectRefList.add(Ray.task(RayDemo::square, i).remote()); } // Get the actual results of the tasks with <code>get</code>. System.out.println(Ray.get(objectRefList)); // [0, 1, 4, 9] }
{ List<ActorHandle<Counter>> counters = new ArrayList<>(); // Create 4 actors from the <code>Counter</code> class. // They will run in remote worker processes. for (int i = 0; i < 4; i++) { counters.add(Ray.actor(Counter::new).remote()); }
// Invoke the <code>increment</code> method on each actor. // This will send an actor task to each remote actor. for (ActorHandle<Counter> counter : counters) { counter.task(Counter::increment).remote(); } // Invoke the <code>read</code> method on each actor, and print the results. List<ObjectRef<Integer>> objectRefList = counters.stream() .map(counter -> counter.task(Counter::read).remote()) .collect(Collectors.toList()); System.out.println(Ray.get(objectRefList)); // [1, 1, 1, 1] } } }
|