mirror of
https://github.com/wassname/ray.git
synced 2026-07-01 02:00:46 +08:00
Remove outdated Java doc and run demo code in CI (#10698)
Resolve Conflicts:
java/tutorial/pom.xml
java/tutorial/pom_template.xml
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package io.ray.docdemo;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* This class contains demo code of the Ray introduction doc
|
||||
* (https://docs.ray.io/en/master/index.html and https://docs.ray.io/en/master/ray-overview/index.html).
|
||||
*
|
||||
* Please keep them in sync.
|
||||
*/
|
||||
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 `square` 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 `get`.
|
||||
System.out.println(Ray.get(objectRefList)); // [0, 1, 4, 9]
|
||||
}
|
||||
|
||||
{
|
||||
List<ActorHandle<Counter>> counters = new ArrayList<>();
|
||||
// Create 4 actors from the `Counter` class.
|
||||
// They will run in remote worker processes.
|
||||
for (int i = 0; i < 4; i++) {
|
||||
counters.add(Ray.actor(Counter::new).remote());
|
||||
}
|
||||
|
||||
// Invoke the `increment` 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 `read` 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]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import java.util.concurrent.TimeUnit;
|
||||
import org.testng.Assert;
|
||||
|
||||
/**
|
||||
* This class contains demo code of the Ray core Using Actors doc (https://docs.ray.io/en/latest/actors.html).
|
||||
* This class contains demo code of the Ray core Using Actors doc (https://docs.ray.io/en/master/actors.html).
|
||||
*
|
||||
* Please keep them in sync.
|
||||
*/
|
||||
|
||||
@@ -11,7 +11,7 @@ import java.util.concurrent.TimeUnit;
|
||||
import org.testng.Assert;
|
||||
|
||||
/**
|
||||
* This class contains demo code of the Ray core walkthrough doc (https://docs.ray.io/en/latest/walkthrough.html).
|
||||
* This class contains demo code of the Ray core walkthrough doc (https://docs.ray.io/en/master/walkthrough.html).
|
||||
*
|
||||
* Please keep them in sync.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user