Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Ray project Ray Ray Init

From Leeroopedia
Knowledge Sources
Domains Distributed_Computing, Runtime_Management
Last Updated 2026-02-13 17:00 GMT

Overview

Concrete tool for bootstrapping the Ray distributed runtime provided by the Ray Java SDK.

Description

The Ray.init() static method initializes the Ray runtime by loading the DefaultRayRuntimeFactory via reflection, creating a RayRuntime instance, and registering a JVM shutdown hook that calls Ray.shutdown(). After initialization, the static Ray.runtime field holds the active runtime, and all subsequent Ray.task(), Ray.actor(), Ray.put(), and Ray.get() calls are dispatched through it.

Usage

Call Ray.init() at the start of any Java application that uses Ray. It must be invoked before any distributed operations. It is safe to call from the main thread; the method is synchronized and idempotent.

Code Reference

Source Location

  • Repository: ray-project/ray
  • File: java/api/src/main/java/io/ray/api/Ray.java
  • Lines: L15-34

Signature

public static void init()

The private overload:

private static synchronized void init(RayRuntimeFactory factory)

Import

import io.ray.api.Ray;

I/O Contract

Inputs

Name Type Required Description
(none) Zero-argument method. Optionally reads system property ray.job.code-search-path and ray.conf config file.

Outputs

Name Type Description
Ray.runtime RayRuntime Static field set to the initialized runtime instance
Shutdown hook Thread JVM shutdown hook registered via Runtime.addShutdownHook

Usage Examples

Basic Initialization

import io.ray.api.Ray;
import io.ray.api.ObjectRef;

public class MyApp {
    public static int square(int x) {
        return x * x;
    }

    public static void main(String[] args) {
        // 1. Initialize Ray runtime
        Ray.init();

        // 2. Submit a remote task
        ObjectRef<Integer> result = Ray.task(MyApp::square, 5).remote();

        // 3. Retrieve the result
        int value = result.get();
        System.out.println("Result: " + value); // 25

        // 4. Shutdown
        Ray.shutdown();
    }
}

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment