Adding a delay without Thread.sleep and a while loop doing nothing

前端 未结 3 749
灰色年华
灰色年华 2021-01-05 02:39

I need to add delay without using Thread.sleep() or a while loop doing nothing. The game im editing(Minecraft) clock runs on \"Ticks\" but they can fluctuate depending on yo

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-05 03:27

    One of approaches is :

    class Timer {
                private static final ScheduledExecutorService scheduledThreadPoolExecutor = Executors.newScheduledThreadPool(10);
        
                private static void doPause(int ms) {
                    try {
                        scheduledThreadPoolExecutor.schedule(() -> {
                        }, ms, TimeUnit.MILLISECONDS).get();
                    } catch (Exception e) {
                        throw new RuntimeException();
                    }
                }
            }
    

    and then you can use Timer.doPause(50) where you need.

提交回复
热议问题