Subclass of
LuaValue that implements
a lua coroutine thread using Java Threads.
A LuaThread is typically created in response to a scripted call to
coroutine.create()
The threads must be initialized with the globals, so that
the global environment may be passed along according to rules of lua.
This is done via the constructor arguments
#LuaThread(Globals) or
#LuaThread(Globals,LuaValue).
The utility classes
org.luaj.vm2.lib.jse.JsePlatform and
org.luaj.vm2.lib.jme.JmePlatform
see to it that this
Globals are initialized properly.
The behavior of coroutine threads matches closely the behavior
of C coroutine library. However, because of the use of Java threads
to manage call state, it is possible to yield from anywhere in luaj.
Each Java thread wakes up at regular intervals and checks a weak reference
to determine if it can ever be resumed. If not, it throws
OrphanedThread which is an
java.lang.Error.
Applications should not catch
OrphanedThread, because it can break
the thread safety of luaj. The value controlling the polling interval
is
#thread_orphan_check_interval and may be set by the user.
There are two main ways to abandon a coroutine. The first is to call
yield() from lua, or equivalently
Globals#yield(Varargs),
and arrange to have it never resumed possibly by values passed to yield.
The second is to throw
OrphanedThread, which should put the thread
in a dead state. In either case all references to the thread must be
dropped, and the garbage collector must run for the thread to be
garbage collected.