mirror of
https://github.com/wassname/ray.git
synced 2026-06-27 22:53:20 +08:00
[Java] New Java actor API (#7414)
This commit is contained in:
@@ -3,7 +3,6 @@ package org.ray.exercise;
|
||||
import java.io.Serializable;
|
||||
import org.ray.api.Ray;
|
||||
import org.ray.api.RayObject;
|
||||
import org.ray.api.annotation.RayRemote;
|
||||
|
||||
/**
|
||||
* Define a remote function, and execute multiple remote functions in parallel.
|
||||
@@ -13,15 +12,12 @@ public class Exercise01 implements Serializable {
|
||||
/**
|
||||
* A plain remote function.
|
||||
*/
|
||||
// `@RayRemote` annotation converts a normal function to a remote function.
|
||||
@RayRemote
|
||||
public static String sayHello() {
|
||||
String ret = "hello";
|
||||
System.out.println(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@RayRemote
|
||||
public static String sayWorld() {
|
||||
String ret = "world!";
|
||||
System.out.println(ret);
|
||||
|
||||
@@ -2,21 +2,18 @@ package org.ray.exercise;
|
||||
|
||||
import org.ray.api.Ray;
|
||||
import org.ray.api.RayObject;
|
||||
import org.ray.api.annotation.RayRemote;
|
||||
|
||||
/**
|
||||
* Execute remote functions in parallel with some dependencies.
|
||||
*/
|
||||
public class Exercise02 {
|
||||
|
||||
@RayRemote
|
||||
public static String sayHello() {
|
||||
String ret = "hello";
|
||||
System.out.println(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@RayRemote
|
||||
public static String sayWorld() {
|
||||
String ret = "world!";
|
||||
System.out.println(ret);
|
||||
@@ -26,7 +23,6 @@ public class Exercise02 {
|
||||
/**
|
||||
* A remote function with dependency.
|
||||
*/
|
||||
@RayRemote
|
||||
public static String merge(String hello, String world) {
|
||||
return hello + "," + world;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package org.ray.exercise;
|
||||
|
||||
import org.ray.api.Ray;
|
||||
import org.ray.api.RayObject;
|
||||
import org.ray.api.annotation.RayRemote;
|
||||
|
||||
/**
|
||||
* Call a remote function from within another remote function.
|
||||
@@ -12,7 +11,6 @@ public class Exercise03 {
|
||||
/**
|
||||
* A remote function which will call another remote function.
|
||||
*/
|
||||
@RayRemote
|
||||
public static String sayHelloWithWorld() {
|
||||
String ret = "hello";
|
||||
System.out.println(ret);
|
||||
@@ -23,7 +21,6 @@ public class Exercise03 {
|
||||
/**
|
||||
* A remote function which will be called by another remote function.
|
||||
*/
|
||||
@RayRemote
|
||||
public static String sayWorld() {
|
||||
String ret = "world!";
|
||||
System.out.println(ret);
|
||||
|
||||
@@ -5,20 +5,17 @@ import java.util.List;
|
||||
import org.ray.api.Ray;
|
||||
import org.ray.api.RayObject;
|
||||
import org.ray.api.WaitResult;
|
||||
import org.ray.api.annotation.RayRemote;
|
||||
|
||||
/**
|
||||
* Use Ray.wait to ignore stragglers
|
||||
*/
|
||||
public class Exercise04 {
|
||||
|
||||
@RayRemote
|
||||
public static String f1() {
|
||||
System.out.println("Executing f1");
|
||||
return "f1";
|
||||
}
|
||||
|
||||
@RayRemote
|
||||
public static String f2() {
|
||||
System.out.println("Executing f2");
|
||||
return "f2";
|
||||
@@ -27,7 +24,6 @@ public class Exercise04 {
|
||||
/**
|
||||
* A slow remote function.
|
||||
*/
|
||||
@RayRemote
|
||||
public static String f3() {
|
||||
System.out.println("Executing f3");
|
||||
try {
|
||||
|
||||
@@ -3,7 +3,6 @@ package org.ray.exercise;
|
||||
import org.ray.api.Ray;
|
||||
import org.ray.api.RayActor;
|
||||
import org.ray.api.RayObject;
|
||||
import org.ray.api.annotation.RayRemote;
|
||||
|
||||
/**
|
||||
* Show usage of actors.
|
||||
@@ -16,9 +15,9 @@ public class Exercise05 {
|
||||
// `Ray.createActor` creates an actor instance.
|
||||
RayActor<Adder> adder = Ray.createActor(Adder::new, 0);
|
||||
// Use `Ray.call(actor, parameters)` to call an actor method.
|
||||
RayObject<Integer> result1 = Ray.call(Adder::add, adder, 1);
|
||||
RayObject<Integer> result1 = adder.call(Adder::add, 1);
|
||||
System.out.println(result1.get());
|
||||
RayObject<Integer> result2 = Ray.call(Adder::add, adder, 10);
|
||||
RayObject<Integer> result2 = adder.call(Adder::add, 10);
|
||||
System.out.println(result2.get());
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
@@ -30,8 +29,6 @@ public class Exercise05 {
|
||||
/**
|
||||
* An example actor.
|
||||
*/
|
||||
// `@RayRemote` annotation also converts a normal class to an actor.
|
||||
@RayRemote
|
||||
public static class Adder {
|
||||
|
||||
public Adder(int initValue) {
|
||||
|
||||
Reference in New Issue
Block a user