---
title: "TruffleThreadBuilder"
description: "A builder for threads that have access to the appropriate TruffleContext."
kind: "class"
id: "com.oracle.truffle.api.TruffleThreadBuilder"
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/TruffleThreadBuilder.html"
---

# ◆ TruffleThreadBuilder

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

Class in [`com.oracle.truffle.api`](index.md). Since 23.0.

```java
public final class TruffleThreadBuilder
```

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

A builder for threads that have access to the appropriate [`TruffleContext`](TruffleContext.md). The context is either the one that corresponds to the [`language environment`](TruffleLanguage.Env.md) that was used to [`create`](TruffleLanguage.Env.md#newTruffleThreadBuilder%28java.lang.Runnable%29) the builder, or the one specified by [`TruffleThreadBuilder.context(TruffleContext)`](TruffleThreadBuilder.md#context%28com.oracle.truffle.api.TruffleContext%29). The method [`TruffleLanguage.Env.newTruffleThreadBuilder(Runnable)`](TruffleLanguage.Env.md#newTruffleThreadBuilder%28java.lang.Runnable%29) is the only way to create the builder.

Threads without an associated context should be created by [`TruffleLanguage.Env.createSystemThread(Runnable)`](TruffleLanguage.Env.md#createSystemThread%28java.lang.Runnable%29).

## Methods

### context(TruffleContext)
<a id="context(com.oracle.truffle.api.TruffleContext)"></a>`public TruffleThreadBuilder context(TruffleContext innerContext)`. Since 23.1. Specifies `TruffleContext` for the threads created by the builder. It has to be an inner context created by `TruffleLanguage.Env.newInnerContextBuilder(String...)`.`build()`. If not specified, the context associated with the language environment that created this builder is used (`TruffleLanguage.Env.getContext()`). Threads without an associated context should be created by `TruffleLanguage.Env.createSystemThread(Runnable)`. Parameters: `innerContext` `TruffleContext`: `TruffleContext` for the threads created by this builder. Cite: `com/oracle/truffle/api/TruffleThreadBuilder.html#context(com.oracle.truffle.api.TruffleContext)`

### threadGroup(ThreadGroup)
<a id="threadGroup(java.lang.ThreadGroup)"></a>`public TruffleThreadBuilder threadGroup(ThreadGroup g)`. Since 23.1. Specifies thread group for the threads created by this builder. Parameters: `g` `ThreadGroup`: thread group for the threads created by this builder. Cite: `com/oracle/truffle/api/TruffleThreadBuilder.html#threadGroup(java.lang.ThreadGroup)`

### stackSize(long)
<a id="stackSize(long)"></a>`public TruffleThreadBuilder stackSize(long size)`. Since 23.1. Specifies stack size for the threads created by this builder. The default is 0 which means that the parameter is ignored. Parameters: `size` `long`: stack size for the threads created by this builder. Cite: `com/oracle/truffle/api/TruffleThreadBuilder.html#stackSize(long)`

### beforeEnter(Runnable)
<a id="beforeEnter(java.lang.Runnable)"></a>`public TruffleThreadBuilder beforeEnter(Runnable r)`. Since 23.1. Specifies before enter notification for the threads created by this builder. The notification runnable is invoked on the thread when the threads starts before the context is entered on the thread. The default value for the notification runnable is `null` which means that no notification is executed. If the notification runnable throws an exception, it is propagated up and can be handled by an [`uncaught exception handler`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Thread.html#setUncaughtExceptionHandler%28java.lang.Thread.UncaughtExceptionHandler%29). Parameters: `r` `Runnable`: before enter notification runnable for the threads created by this builder. Cite: `com/oracle/truffle/api/TruffleThreadBuilder.html#beforeEnter(java.lang.Runnable)`

### afterLeave(Runnable)
<a id="afterLeave(java.lang.Runnable)"></a>`public TruffleThreadBuilder afterLeave(Runnable r)`. Since 23.1. Specifies after leave notification for the threads created by this builder. The notification runnable is invoked on the thread after the thread leaves the context for the last time, i.e. the thread is about to finish. The default value for the notification runnable is `null` which means that no notification is executed. If the notification runnable throws an exception, it is propagated up and can be handled by an [`uncaught exception handler`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Thread.html#setUncaughtExceptionHandler%28java.lang.Thread.UncaughtExceptionHandler%29). Parameters: `r` `Runnable`: after leave notification runnable for the threads created by this builder. Cite: `com/oracle/truffle/api/TruffleThreadBuilder.html#afterLeave(java.lang.Runnable)`

### virtual(boolean)
<a id="virtual(boolean)"></a>`public TruffleThreadBuilder virtual(boolean v)`. Since 24.1. Specifies whether to create a virtual thread (Thread#ofVirtual()) or a regular platform thread (the default) for the threads created by this builder. Parameters: `v` `boolean`: whether to create a virtual thread. Cite: `com/oracle/truffle/api/TruffleThreadBuilder.html#virtual(boolean)`

### build()
<a id="build()"></a>`public Thread build()`. Since 23.1. Creates a new thread based on the parameters specified by this builder. The thread is `initialized` when it is [`started`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Thread.html#start%28%29), and `finalized` and `disposed` when it finishes its execution. It is recommended to set an [`uncaught exception handler`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Thread.html#setUncaughtExceptionHandler%28java.lang.Thread.UncaughtExceptionHandler%29) for the created thread. For example the thread can throw an uncaught exception if one of the initialized language contexts don't support execution on this thread. The language that created and started the thread is responsible to stop and join it during the `finalizeContext`, otherwise an internal error is thrown. It's not safe to use the [`ExecutorService.awaitTermination(long, java.util.concurrent.TimeUnit)`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/concurrent/ExecutorService.html#awaitTermination%28long,java.util.concurrent.TimeUnit%29) to detect Thread termination as the polyglot thread may be cancelled before executing the executor worker. A typical implementation looks like: `TruffleLanguageSnippets.AsyncThreadLanguage.finalizeContext`. Cite: `com/oracle/truffle/api/TruffleThreadBuilder.html#build()`

## 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)
