Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Gnakkk opened this issue May 14, 2021 · 2 comments
Open

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

Gnakkk opened this issue May 14, 2021 · 2 comments
Labels

Comments

@Gnakkk
Copy link

Gnakkk commented May 14, 2021

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)
有什么好方法吗?

@Gnakkk Gnakkk closed this as completed May 14, 2021
@Gnakkk Gnakkk reopened this May 14, 2021
@Gnakkk Gnakkk changed the title 请问如何正确实现sleep延时效果 请问如何强行终止lua函数线程 May 14, 2021
@Gnakkk Gnakkk changed the title 请问如何强行终止lua函数线程 请问如何正确实现sleep延时效果 May 14, 2021
@ichenhe
Copy link
Owner

ichenhe commented Oct 9, 2021

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

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

@bamboo98
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants