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

前端 未结 3 734
灰色年华
灰色年华 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:21

    long start = new Date().getTime();
    while(new Date().getTime() - start < 1000L){}
    

    is the simplest solution I can think about.

    Still, the heap might get polluted with a lot of unreferenced Date objects, which, depending on how often you get to create such a pseudo-delay, might increase the GC overhead.

    At the end of the day, you have to know that this is no better solution in terms of processor usage, compared to the Thread.sleep() solution.

提交回复
热议问题