Class LuaThread

All Implemented Interfaces:
com.esotericsoftware.kryo.KryoSerializable

public class LuaThread extends LuaValue implements com.esotericsoftware.kryo.KryoSerializable
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).

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 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.

See Also:
  • Field Details

    • s_metatable

      public static LuaValue s_metatable
      Shared metatable for lua threads.
    • coroutine_count

      public static int coroutine_count
      The current number of coroutines. Should not be set.
    • thread_orphan_check_interval

      public static long thread_orphan_check_interval
      Polling interval, in milliseconds, which each thread uses while waiting to return from a yielded state to check if the lua threads is no longer referenced and therefore should be garbage collected. A short polling interval for many threads will consume server resources. Orphaned threads cannot be detected and collected unless garbage collection is run. This can be changed by Java startup code if desired.
    • STATUS_INITIAL

      public static final int STATUS_INITIAL
      See Also:
    • STATUS_SUSPENDED

      public static final int STATUS_SUSPENDED
      See Also:
    • STATUS_RUNNING

      public static final int STATUS_RUNNING
      See Also:
    • STATUS_NORMAL

      public static final int STATUS_NORMAL
      See Also:
    • STATUS_DEAD

      public static final int STATUS_DEAD
      See Also:
    • STATUS_NAMES

      public static final String[] STATUS_NAMES
    • state

      public LuaThread.State state
    • MAX_CALLSTACK

      public static final int MAX_CALLSTACK
      See Also:
    • callstack

      public Object callstack
      Thread-local used by DebugLib to store debugging state. This is an opaque value that should not be modified by applications.
    • globals

      public Globals globals
    • errorfunc

      public LuaValue errorfunc
      Error message handler for this thread, if any.
  • Constructor Details

    • LuaThread

      public LuaThread(Globals globals)
      Private constructor for main thread only
    • LuaThread

      public LuaThread(Globals globals, LuaValue func)
      Create a LuaThread around a function and environment
      Parameters:
      func - The function to execute
  • Method Details