---
title: "ThreadLocalAction"
description: "Represents an action that is executed at a safepoint location of the guest language execution."
kind: "abstract class"
id: "com.oracle.truffle.api.ThreadLocalAction"
module: "truffle-api"
version: "25.6.0-elide.4"
canonical: "https://graalvm.elide.help/25.6.0-elide.4/api/truffle-api/com/oracle/truffle/api/ThreadLocalAction.html"
---

# ◆ ThreadLocalAction

[truffle-api](../../../../index.md) › [com.oracle.truffle.api](index.md) · Every page: [llms.txt](../../../../llms.txt)

Abstract class in [`com.oracle.truffle.api`](index.md). Since 21.1.

```java
public abstract class ThreadLocalAction
```

Inheritance: [`Object`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Object.html) > `ThreadLocalAction`

Represents an action that is executed at a [`safepoint`](TruffleSafepoint.md) location of the guest language execution. Thread local actions can be submitted by [`languages`](TruffleLanguage.Env.md#submitThreadLocal%28java.lang.Thread[],com.oracle.truffle.api.ThreadLocalAction%29) or [`instruments`](instrumentation/TruffleInstrument.Env.md#submitThreadLocal%28com.oracle.truffle.api.TruffleContext,java.lang.Thread[],com.oracle.truffle.api.ThreadLocalAction%29). When an action is submitted it will be [`performed`](ThreadLocalAction.md#perform%28com.oracle.truffle.api.ThreadLocalAction.Access%29) locally on the threads they are submitted to. After submitting a thread local action a [`Future`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/concurrent/Future.html) is returned that allows to wait for and cancel the submitted action.

Thread local actions can be configured to allow side-effects using the constructor of the action. If side-effects are allowed (`true`), then the thread-local action is allowed to throw non-internal guest language exceptions from the action and modify the observable guest application state. Otherwise, the action is not allowed to modify the observable state, and any non-internal guest language exception will be transformed to an internal error of type [`AssertionError`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/AssertionError.html). Side-effects may be temporarily [`disabled`](TruffleSafepoint.md#setAllowSideEffects%28boolean%29) by the guest language.

Thread local actions can also be set to be executed in a synchronous or asynchronous way. A submitted synchronous thread-local action waits until it is started on all threads that it was submitted on. Then the action is [`performed`](ThreadLocalAction.md#perform%28com.oracle.truffle.api.ThreadLocalAction.Access%29) on all threads. After they were performed on a thread, the action waits for all threads to complete. No synchronous thread-local actions can be submitted while performing synchronous actions, as this may lead to deadlocks. If a synchronous event is submitted during a synchronous event, then a [`IllegalStateException`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/IllegalStateException.html) is thrown when the action is submitted. Asynchronous thread-local actions might start and complete to perform independently of each other. There is no restriction on how they may be submitted.

Thread local actions are guaranteed to be executed in the same order as they were submitted for a context. If a context has pending thread-local actions, the actions may be canceled when the context is canceled or closed invalid. Exceptions thrown by the action will be forwarded handled by the guest language implementation. The only exception is for truffle exceptions that are thrown for non-side-effecting events.

Notifications of blocking:

[`notifyBlocked(Access)`](ThreadLocalAction.md#notifyBlocked%28com.oracle.truffle.api.ThreadLocalAction.Access%29) and [`notifyUnblocked(Access)`](ThreadLocalAction.md#notifyUnblocked%28com.oracle.truffle.api.ThreadLocalAction.Access%29) notify thread local actions that their processing has been blocked/unblocked due to [`a blocked call`](TruffleSafepoint.md#setBlockedFunction%28com.oracle.truffle.api.nodes.Node,com.oracle.truffle.api.TruffleSafepoint.Interrupter,com.oracle.truffle.api.TruffleSafepoint.InterruptibleFunction,T,java.lang.Runnable,java.util.function.Consumer%29). [`notifyBlocked(Access)`](ThreadLocalAction.md#notifyBlocked%28com.oracle.truffle.api.ThreadLocalAction.Access%29) is called for each pending action at the beginning of a blocked call and for each of the continuations of the blocked call after it is interrupted and thread local actions are processed. [`ThreadLocalAction.notifyUnblocked(Access)`](ThreadLocalAction.md#notifyUnblocked%28com.oracle.truffle.api.ThreadLocalAction.Access%29) is called for each pending action at the end of a blocked call and also right after each of its interruptions, before thread local actions are processed and the blocked call continues.

In case a thread local action is submitted during a blocked call, the call is interrupted and [`notifyUnblocked(Access)`](ThreadLocalAction.md#notifyUnblocked%28com.oracle.truffle.api.ThreadLocalAction.Access%29) is called without a previous call to [`notifyBlocked(Access)`](ThreadLocalAction.md#notifyBlocked%28com.oracle.truffle.api.ThreadLocalAction.Access%29). Recurring thread local actions do not repeatedly interrupt a blocked call - a blocked call is not interrupted if all pending actions are recurring actions submitted before the blocked call. New submissions of thread local actions still interrupt blocked calls, no matter if the new thread local actions are recurring or not. When a blocked call is interrupted, all pending actions are processed no matter if they are recurring or not.

The notifications of blocking are especially useful for recurring thread local actions as those actions don't interrupt blocked calls and the notifications inform them about the potentially long time intervals when those actions are not executed due to the blocked calls. Non-recurring thread local actions mainly benefit from the [`ThreadLocalAction.notifyUnblocked(Access)`](ThreadLocalAction.md#notifyUnblocked%28com.oracle.truffle.api.ThreadLocalAction.Access%29) notification telling them that they interrupted a blocked call. For example, a safepoint sampler might want to exclude the samples from blocked calls.

Example Usage:

```java
Env env; // language or instrument environment

env.submitThreadLocal(null, new ThreadLocalAction(true, true) {
    @Override
    protected void perform(Access access) {
        assert access.getThread() == Thread.currentThread();
    }

    @Override
    protected void notifyBlocked(Access access) {
        assert access.getThread() == Thread.currentThread();
    }

    @Override
    protected void notifyUnblocked(Access access) {
        assert access.getThread() == Thread.currentThread();
    }

    @Override
    protected String name() {
       return "MyAction"
    }

});
```

Further information can be found in the [safepoint tutorial](http://github.com/oracle/graal/blob/master/truffle/docs/Safepoints.md).

**See also**

- [`TruffleSafepoint`](TruffleSafepoint.md)
- [`TruffleLanguage.Env.submitThreadLocal`](TruffleLanguage.Env.md#submitThreadLocal%28java.lang.Thread[],com.oracle.truffle.api.ThreadLocalAction%29)
- [`com.oracle.truffle.api.instrumentation.TruffleInstrument.Env.submitThreadLocal`](instrumentation/TruffleInstrument.Env.md#submitThreadLocal%28com.oracle.truffle.api.TruffleContext,java.lang.Thread[],com.oracle.truffle.api.ThreadLocalAction%29)

## Nested types

- [`ThreadLocalAction.Access`](ThreadLocalAction.Access.md) (abstract class): Argument class for ThreadLocalAction.perform(Access).

## Constructors

### ThreadLocalAction(boolean, boolean)
<a id="&lt;init&gt;(boolean,boolean)"></a>`protected ThreadLocalAction(boolean hasSideEffects, boolean synchronous)`. Since 21.1. Creates a new thread local action. Parameters: `hasSideEffects` `boolean`: true if the event may have side-effects else false. `synchronous` `boolean`: true if the event should run synchronous else the event will run asynchronous. See also: `ThreadLocalAction`. Cite: `com/oracle/truffle/api/ThreadLocalAction.html#<init>(boolean,boolean)`

### ThreadLocalAction(boolean, boolean, boolean)
<a id="&lt;init&gt;(boolean,boolean,boolean)"></a>`protected ThreadLocalAction(boolean hasSideEffects, boolean synchronous, boolean recurring)`. Since 21.1. Creates a new thread local action. Parameters: `hasSideEffects` `boolean`: true if the event may have side-effects else false. `synchronous` `boolean`: true if the event should run synchronous else the event will run asynchronous. `recurring` `boolean`: true if the event should be rescheduled until cancelled, else false. See also: `ThreadLocalAction`. Cite: `com/oracle/truffle/api/ThreadLocalAction.html#<init>(boolean,boolean,boolean)`

## Methods

### perform(ThreadLocalAction.Access)
<a id="perform(com.oracle.truffle.api.ThreadLocalAction.Access)"></a>`protected abstract void perform(ThreadLocalAction.Access access)`. Since 21.1. Performs the thread local action on a given thread. Parameters: `access` `ThreadLocalAction.Access`: allows access to the current thread and the current code location. See also: `ThreadLocalAction`. Cite: `com/oracle/truffle/api/ThreadLocalAction.html#perform(com.oracle.truffle.api.ThreadLocalAction.Access)`

### notifyBlocked(ThreadLocalAction.Access)
<a id="notifyBlocked(com.oracle.truffle.api.ThreadLocalAction.Access)"></a>`protected void notifyBlocked(ThreadLocalAction.Access access)`. Since 24.2. Callback for notifying the thread local action that its processing has been blocked due to `a blocked call`. Parameters: `access` `ThreadLocalAction.Access`: allows access to the current thread and the current code location. See also: `ThreadLocalAction`. Cite: `com/oracle/truffle/api/ThreadLocalAction.html#notifyBlocked(com.oracle.truffle.api.ThreadLocalAction.Access)`

### notifyUnblocked(ThreadLocalAction.Access)
<a id="notifyUnblocked(com.oracle.truffle.api.ThreadLocalAction.Access)"></a>`protected void notifyUnblocked(ThreadLocalAction.Access access)`. Since 24.2. Callback for notifying the thread local action that its processing has been unblocked during or while leaving `a blocked call`. Parameters: `access` `ThreadLocalAction.Access`: allows access to the current thread and the current code location. See also: `ThreadLocalAction`. Cite: `com/oracle/truffle/api/ThreadLocalAction.html#notifyUnblocked(com.oracle.truffle.api.ThreadLocalAction.Access)`

## Inherited members

- From class [`Object`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Object.html): [`clone`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Object.html#clone%28%29), [`equals`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Object.html#equals%28java.lang.Object%29), [`finalize`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Object.html#finalize%28%29), [`getClass`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Object.html#getClass%28%29), [`hashCode`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Object.html#hashCode%28%29), [`notify`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Object.html#notify%28%29), [`notifyAll`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Object.html#notifyAll%28%29), [`toString`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Object.html#toString%28%29), [`wait`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Object.html#wait%28%29), [`wait`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Object.html#wait%28long%29), [`wait`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Object.html#wait%28long,int%29)
