简体   繁体   English

等待预成型 function 不冻结进程

[英]Wait to preform function without freezing the process

I want to wait 300ms after 2 lines run to run the same 2 lines again, without freezing the thread.我想在 2 行运行后等待 300 毫秒再次运行相同的 2 行,而不冻结线程。

wait(300); and Thread.sleep(300);Thread.sleep(300); , along with some loop I found on SO (below) either freeze the thread, exit cleanly(?) or lag the thread by running the function a million times. ,以及我在 SO(如下)上发现的一些循环,要么冻结线程,干净地退出(?),要么通过运行 function 一百万次来滞后线程。

I want to wait 300 milliseconds and then run我想等待 300 毫秒然后运行

mc.player.rotationPitch = 90;
mc.playerController.processRightClick(mc.player, mc.world, hand);

without freezing the thread, as sometimes it doesn't time properly if the thread is frozen, and it's annoying for the user if it's going to freeze every time.不冻结线程,因为如果线程被冻结,有时它不会正确计时,如果每次都会冻结,这对用户来说很烦人。

I've tried wait, Thread.sleep , TimeUnit.MILLISECONDS.sleep and我试过wait, Thread.sleepTimeUnit.MILLISECONDS.sleep

                long lastNanoTime = System.nanoTime();
                long nowTime = System.nanoTime();
                while(nowTime/1000000 - lastNanoTime /1000000 < 300 )
                {
                    nowTime = System.nanoTime();
                    System.out.println("KAMI: Tried to pick up bucket");

                } 

I've already shown the relevant examples above.我已经展示了上面的相关示例。 Full code is here 完整代码在这里

Expected: thread works normally, and my 2 lines, (rotation pitch and right click) run 300 milliseconds after the previous rotation pitch and right click预期:线程正常工作,我的2行,(旋转间距和右键单击)在上一个旋转间距和右键单击后运行300毫秒

Actual results: commented in code.实际结果:在代码中注释。 Depending on the method used thread either lags, exits or crashes根据使用的方法,线程会滞后、退出或崩溃

You will need another thread to do this "without freezing" the current thread.您将需要另一个线程来“不冻结”当前线程。 This can be done quite easily, something like:这可以很容易地完成,例如:

import java.lang.Thread;

public class Main {

    public static abstract class After extends Thread {

        private int sleep = 0;

        public After(int sleep) {
            this.sleep = sleep;
        }

        public void run() {
            try {
                Thread.sleep(this.sleep);
            } catch(InterruptedException e) {
                //do something with e
            }
            this.after();
        }

        public abstract void after();
    }

    public static void main(String[] args) {
        After after = new After(300) {
            public void after() {
                //mc.player.rotationPitch = 90;
                //mc.playerController.processRightClick(mc.player, mc.world, hand);
                System.out.println("testing");
            }
        };
        after.start(); //this will execute the code in 300 ms

        //do what ever you want to do during the 300ms

        after.join(); //join all threads at the end of your code
        System.out.println("done");
    }
}

Use After when you want to create a delay.当您想要创建延迟时使用After Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM