Skip to content

请问如何正确实现sleep延时效果 #35

Open

Description

@Gnakkk
JavaFunction sleep = new JavaFunction(L) {
                @Override
                public int execute() throws LuaException {
                    int time = L.toInteger(2);
                    try {
                        Thread.sleep(time);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    return 1; 
                }
            };
            sleep.register("sleep");

例子:
print(1)
sleep(6000)
print(2)

问题:sleep()和print等函数一起使用时,延时6秒后才同时打印出1和2(而不是先打1过6秒后打2)
有什么好方法吗?

Activity

  1. reopened this on May 14, 2021
  2. changed the title [-]请问如何正确实现sleep延时效果[/-] [+]请问如何强行终止lua函数线程[/+] on May 14, 2021
  3. changed the title [-]请问如何强行终止lua函数线程[/-] [+]请问如何正确实现sleep延时效果[/+] on May 14, 2021
  4. ichenhe commented on Oct 9, 2021

    @ichenhe
    Owner

    lua 没有 sleep 的完美方案。我也是苦恼了很久。一个变通办法是利用 callback,具体代码见 demo 里的 callback。

    谁有更好的方案欢迎补充~

  5. bamboo98 commented on Mar 21, 2022

    @bamboo98

    有没有可能,我是说可能,
    你在sleep之前,先flush一下print的缓冲区就行了呢?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions