public final class TruffleThreadBuilder extends Object
TruffleContext
. The context is
either the one that corresponds to the language environment
that was
used to create
the builder, or the
one specified by TruffleThreadBuilder.context(TruffleContext)
. The method
TruffleLanguage.Env.newTruffleThreadBuilder(Runnable)
is the only way to create the
builder.
Threads without an associated context should be created by
TruffleLanguage.Env.createSystemThread(Runnable)
.
Modifier and Type | Method and Description |
---|---|
TruffleThreadBuilder |
afterLeave(Runnable r)
Specifies after leave notification for the threads created by this builder.
|
TruffleThreadBuilder |
beforeEnter(Runnable r)
Specifies before enter notification for the threads created by this builder.
|
Thread |
build()
Creates a new thread based on the parameters specified by this builder.
|
TruffleThreadBuilder |
context(TruffleContext innerContext)
Specifies
TruffleContext for the threads created by the builder. |
TruffleThreadBuilder |
stackSize(long size)
Specifies stack size for the threads created by this builder.
|
TruffleThreadBuilder |
threadGroup(ThreadGroup g)
Specifies thread group for the threads created by this builder.
|
public TruffleThreadBuilder context(TruffleContext innerContext)
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)
.
innerContext
- TruffleContext
for the threads created by this builder.public TruffleThreadBuilder threadGroup(ThreadGroup g)
g
- thread group for the threads created by this builder.public TruffleThreadBuilder stackSize(long size)
size
- stack size for the threads created by this builder.public TruffleThreadBuilder beforeEnter(Runnable r)
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
r
- before enter notification runnable for the threads created by this builder.public TruffleThreadBuilder afterLeave(Runnable r)
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
r
- after leave notification runnable for the threads created by this builder.public Thread build()
It is recommended to set an
uncaught
exception handler
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)
to detect
Thread termination as the polyglot thread may be cancelled before executing the executor
worker.
A typical implementation looks like:
class AsyncThreadLanguage extendsTruffleLanguage
<Context
> { @Override
protectedContext
createContext(TruffleLanguage.Env
env) { return newContext
(env); } @Override
protected boolean isThreadAccessAllowed(Thread
thread, boolean singleThreaded) { // allow access from any thread instead of just one return true; } @Override
protected void initializeContext(Context
context) throwsException
{ // create and start a Thread for the asynchronous task // remeber the Thread reference to stop and join it in // the finalizeContextThread
t = context.env.newTruffleThreadBuilder(newRunnable
() { @Override
public void run() { // asynchronous task } }).build(); context.startedThreads.add(t); t.start(); } @Override
protected void finalizeContext(Context
context) { // stop and join all the created Threads boolean interrupted = false; for (int i = 0; i < context.startedThreads.size();) {Thread
threadToJoin = context.startedThreads.get(i); try { if (threadToJoin !=Thread
.currentThread()) { threadToJoin.interrupt(); threadToJoin.join(); } i++; } catch (InterruptedException
ie) { interrupted = true; } } if (interrupted) {Thread
.currentThread().interrupt(); } } }