mirror of
https://github.com/wassname/ray.git
synced 2026-06-27 22:38:16 +08:00
[Java] Refactor java api (#8858)
This commit is contained in:
@@ -29,8 +29,8 @@ public class Exercise01 implements Serializable {
|
||||
// Use `Ray.init` to initialize the Ray runtime.
|
||||
Ray.init();
|
||||
// Use `Ray.call` to call a remote function.
|
||||
ObjectRef<String> hello = Ray.call(Exercise01::sayHello);
|
||||
ObjectRef<String> world = Ray.call(Exercise01::sayWorld);
|
||||
ObjectRef<String> hello = Ray.task(Exercise01::sayHello).remote();
|
||||
ObjectRef<String> world = Ray.task(Exercise01::sayWorld).remote();
|
||||
System.out.println("First remote call result:" + hello.get());
|
||||
System.out.println("Second remote call result:" + world.get());
|
||||
} catch (Throwable t) {
|
||||
|
||||
@@ -28,10 +28,10 @@ public class Exercise02 {
|
||||
}
|
||||
|
||||
public static String sayHelloWorld() {
|
||||
ObjectRef<String> hello = Ray.call(Exercise02::sayHello);
|
||||
ObjectRef<String> world = Ray.call(Exercise02::sayWorld);
|
||||
ObjectRef<String> hello = Ray.task(Exercise02::sayHello).remote();
|
||||
ObjectRef<String> world = Ray.task(Exercise02::sayWorld).remote();
|
||||
// Pass unfinished results as the parameters to another remote function.
|
||||
return Ray.call(Exercise02::merge, hello, world).get();
|
||||
return Ray.task(Exercise02::merge, hello, world).remote().get();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
@@ -14,7 +14,7 @@ public class Exercise03 {
|
||||
public static String sayHelloWithWorld() {
|
||||
String ret = "hello";
|
||||
System.out.println(ret);
|
||||
ObjectRef<String> world = Ray.call(Exercise03::sayWorld);
|
||||
ObjectRef<String> world = Ray.task(Exercise03::sayWorld).remote();
|
||||
return ret + "," + world.get();
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ public class Exercise03 {
|
||||
public static void main(String[] args) throws Exception {
|
||||
try {
|
||||
Ray.init();
|
||||
String helloWithWorld = Ray.call(Exercise03::sayHelloWithWorld).get();
|
||||
String helloWithWorld = Ray.task(Exercise03::sayHelloWithWorld).remote().get();
|
||||
System.out.println(helloWithWorld);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
|
||||
@@ -39,9 +39,9 @@ public class Exercise04 {
|
||||
try {
|
||||
Ray.init();
|
||||
List<ObjectRef<String>> waitList = ImmutableList.of(
|
||||
Ray.call(Exercise04::f1),
|
||||
Ray.call(Exercise04::f2),
|
||||
Ray.call(Exercise04::f3)
|
||||
Ray.task(Exercise04::f1).remote(),
|
||||
Ray.task(Exercise04::f2).remote(),
|
||||
Ray.task(Exercise04::f3).remote()
|
||||
);
|
||||
// Ray.wait will block until specified number of results are ready
|
||||
// or specified timeout have passed.
|
||||
|
||||
@@ -13,11 +13,11 @@ public class Exercise05 {
|
||||
try {
|
||||
Ray.init();
|
||||
// `Ray.createActor` creates an actor instance.
|
||||
ActorHandle<Adder> adder = Ray.createActor(Adder::new, 0);
|
||||
// Use `Ray.call(actor, parameters)` to call an actor method.
|
||||
ObjectRef<Integer> result1 = adder.call(Adder::add, 1);
|
||||
ActorHandle<Adder> adder = Ray.actor(Adder::new, 0).remote();
|
||||
// Use `Ray.task(actor, parameters).remote()` to call an actor method.
|
||||
ObjectRef<Integer> result1 = adder.task(Adder::add, 1).remote();
|
||||
System.out.println(result1.get());
|
||||
ObjectRef<Integer> result2 = adder.call(Adder::add, 10);
|
||||
ObjectRef<Integer> result2 = adder.task(Adder::add, 10).remote();
|
||||
System.out.println(result2.get());
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
|
||||
Reference in New Issue
Block a user