import org.luaj.vm2.Globals;
import org.luaj.vm2.libs.jse.JsePlatform;
import org.luaj.vm2.libs.jse.LuaScheduler;

public class SampleLuaScheduler {

	public static void main(String[] args) throws Exception {
		Globals globals = JsePlatform.standardGlobals();
		LuaScheduler scheduler = new LuaScheduler(globals);

		globals.load(
			"spawn(function()\n" +
			"  print('task1 start')\n" +
			"  wait(250)\n" +
			"  print('task1 after wait')\n" +
			"end)\n" +
			"spawn(function()\n" +
			"  print('task2 start')\n" +
			"  yield()\n" +
			"  print('task2 next tick')\n" +
			"end)\n",
			"sample_scheduler.lua"
		).call();

		while (scheduler.hasPendingTasks()) {
			if (scheduler.runReadyTasks() == 0) {
				Thread.sleep(scheduler.millisUntilNextTask());
			}
		}
	}
}
