Implementation:Ray project Ray Ray Get And Wait
| Knowledge Sources | |
|---|---|
| Domains | Distributed_Computing, Data_Transfer |
| Last Updated | 2026-02-13 17:00 GMT |
Overview
Concrete tool for retrieving distributed objects from the Ray object store provided by the Ray Java SDK.
Description
The Ray.get() and Ray.wait() static methods provide object retrieval from the distributed object store. Ray.get() blocks until the referenced object is available and returns the deserialized Java object. Ray.wait() returns a WaitResult containing lists of ready and unready object references, enabling efficient batch processing. Both methods delegate through AbstractRayRuntime to ObjectStore (backed by NativeObjectStore via JNI in cluster mode).
Usage
Use Ray.get() when you need the actual value of a computation result and are ready to block. Use Ray.wait() when processing multiple concurrent tasks and want to handle results as they arrive rather than waiting for all of them.
Code Reference
Source Location
- Repository: ray-project/ray
- File: java/api/src/main/java/io/ray/api/Ray.java (L86-173 for get/wait methods)
- File: java/api/src/main/java/io/ray/api/ObjectRef.java (L1-24, get interface)
- File: java/runtime/src/main/java/io/ray/runtime/object/ObjectStore.java (L1-259)
Signature
// Single object get
public static <T> T get(ObjectRef<T> objectRef)
public static <T> T get(ObjectRef<T> objectRef, long timeoutMs)
// Batch get
public static <T> List<T> get(List<ObjectRef<T>> objectList)
public static <T> List<T> get(List<ObjectRef<T>> objectList, long timeoutMs)
// Wait for readiness
public static <T> WaitResult<T> wait(
List<ObjectRef<T>> waitList, int numReturns, int timeoutMs, boolean fetchLocal)
public static <T> WaitResult<T> wait(List<ObjectRef<T>> waitList, int numReturns, int timeoutMs)
public static <T> WaitResult<T> wait(List<ObjectRef<T>> waitList, int numReturns)
public static <T> WaitResult<T> wait(List<ObjectRef<T>> waitList)
// ObjectRef instance method
T ObjectRef.get()
T ObjectRef.get(long timeoutMs)
Import
import io.ray.api.Ray;
import io.ray.api.ObjectRef;
import io.ray.api.WaitResult;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| objectRef | ObjectRef<T> | Yes | Future reference from a task submission |
| timeoutMs | long | No | Maximum wait time in milliseconds (default: Integer.MAX_VALUE) |
| numReturns | int | No (wait only) | Number of ready objects to wait for |
| fetchLocal | boolean | No (wait only) | If true, fetch object to local node before marking ready (default: true) |
Outputs
| Name | Type | Description |
|---|---|---|
| get() returns | T | The deserialized Java result object |
| wait() returns | WaitResult<T> | Contains ready (List<ObjectRef<T>>) and unready (List<ObjectRef<T>>) |
Usage Examples
Blocking Get
import io.ray.api.Ray;
import io.ray.api.ObjectRef;
public class GetExample {
public static int compute(int x) { return x * x; }
public static void main(String[] args) {
Ray.init();
ObjectRef<Integer> ref = Ray.task(GetExample::compute, 7).remote();
// Blocking get — waits until result is ready
int result = Ray.get(ref);
// Or equivalently: int result = ref.get();
System.out.println(result); // 49
Ray.shutdown();
}
}
Wait for Multiple Results
import io.ray.api.Ray;
import io.ray.api.ObjectRef;
import io.ray.api.WaitResult;
import java.util.ArrayList;
import java.util.List;
public class WaitExample {
public static int slowCompute(int x) {
try { Thread.sleep(x * 1000); } catch (Exception e) {}
return x;
}
public static void main(String[] args) {
Ray.init();
List<ObjectRef<Integer>> refs = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
refs.add(Ray.task(WaitExample::slowCompute, i).remote());
}
// Wait for at least 2 results with a 3-second timeout
WaitResult<Integer> result = Ray.wait(refs, 2, 3000);
System.out.println("Ready: " + result.getReady().size());
System.out.println("Unready: " + result.getUnready().size());
Ray.shutdown();
}
}