Implementation:Ray project Ray Ray Task Remote
| Knowledge Sources | |
|---|---|
| Domains | Distributed_Computing, Task_Scheduling |
| Last Updated | 2026-02-13 17:00 GMT |
Overview
Concrete tool for submitting functions as distributed remote tasks provided by the Ray Java SDK.
Description
The Ray.task() static method creates a TaskCaller builder, and calling .remote() on it submits the function for distributed execution. Internally, Ray.task() is defined on RayCall (the superclass of Ray) with overloaded variants for 0 to 6 parameters. TaskCaller.remote() delegates to Ray.internal().call(func, args, options), which resolves the function via FunctionManager, serializes arguments via ArgumentsBuilder.wrap(), and dispatches to NativeTaskSubmitter.submitTask() through JNI.
Usage
Use Ray.task() to submit any stateless function for remote execution on the cluster. Pass the function as a method reference and its arguments as subsequent parameters. Call .remote() to actually submit the task and receive an ObjectRef for the result.
Code Reference
Source Location
- Repository: ray-project/ray
- File: java/api/src/main/java/io/ray/api/RayCall.java (task factory methods, L38-1308)
- File: java/api/src/main/java/io/ray/api/call/TaskCaller.java (remote() call, L12-31)
- File: java/runtime/src/main/java/io/ray/runtime/task/NativeTaskSubmitter.java (JNI submission, L1-156)
Signature
// Factory method (on RayCall, inherited by Ray) — 1-param variant shown
public static <T0, R> TaskCaller<R> task(RayFunc1<T0, R> f, T0 t0)
// TaskCaller.remote() — submits the task
public ObjectRef<R> remote()
Overloaded variants exist for 0 to 6 parameters (Ray.task(func), Ray.task(func, t0), ..., Ray.task(func, t0, t1, t2, t3, t4, t5)).
Import
import io.ray.api.Ray;
import io.ray.api.ObjectRef;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| f | RayFuncN<..., R> | Yes | A serializable function reference (method reference or lambda) |
| args | T0, T1, ... | Depends on arity | Function arguments; can include ObjectRef<T> for pass-by-reference |
| CallOptions | CallOptions | No | Optional resource requirements, task name, placement group (set via builder before .remote()) |
Outputs
| Name | Type | Description |
|---|---|---|
| result | ObjectRef<R> | A future reference to the result object in the distributed object store |
Usage Examples
Basic Task Submission
import io.ray.api.Ray;
import io.ray.api.ObjectRef;
public class TaskExample {
public static String greet(String name) {
return "Hello, " + name + "!";
}
public static void main(String[] args) {
Ray.init();
// Submit a remote task
ObjectRef<String> ref = Ray.task(TaskExample::greet, "World").remote();
// Get the result (blocks until ready)
String result = ref.get();
System.out.println(result); // "Hello, World!"
Ray.shutdown();
}
}
Chaining Tasks with ObjectRef
import io.ray.api.Ray;
import io.ray.api.ObjectRef;
public class ChainExample {
public static int increment(int x) { return x + 1; }
public static int doubleIt(int x) { return x * 2; }
public static void main(String[] args) {
Ray.init();
// Chain tasks: increment(5) -> doubleIt(result)
ObjectRef<Integer> step1 = Ray.task(ChainExample::increment, 5).remote();
// Pass ObjectRef directly — no data movement until needed
ObjectRef<Integer> step2 = Ray.task(ChainExample::doubleIt, step1).remote();
System.out.println(step2.get()); // 12
Ray.shutdown();
}
}