Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 your FPS.

public void onTick() {//Called every "Tick"
    if(variable){ //If my variable is true
            boolean = true; //Setting my boolean to true
            /**
            *Doing a bunch of things.
            **/
            //I need a delay for about one second here.
            boolean = false; //Setting my boolean to false;
    }
}

The reason why i need a delay is because if i dont have one the code runs too fast and misses it and does not toggle.

like image 434
user3166950 Avatar asked Jan 06 '14 21:01

user3166950


People also ask

Why thread sleep should not be used?

Thread. sleep is bad! It blocks the current thread and renders it unusable for further work.

How do you add a delay in Java?

The easiest way to delay a java program is by using Thread. sleep() method. The sleep() method is present in the Thread class. It simply pauses the current thread to sleep for a specific time.

What happens when a thread is put to sleep?

Thread. sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.

Does thread sleep block thread?

Sleep method. Calling the Thread. Sleep method causes the current thread to immediately block for the number of milliseconds or the time interval you pass to the method, and yields the remainder of its time slice to another thread. Once that interval elapses, the sleeping thread resumes execution.


3 Answers

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.

like image 123
Andrei Nicusan Avatar answered Nov 15 '22 17:11

Andrei Nicusan


Something like the following should give you the delay you need without holding up the game thread:

private final long PERIOD = 1000L; // Adjust to suit timing
private long lastTime = System.currentTimeMillis() - PERIOD;

public void onTick() {//Called every "Tick"
    long thisTime = System.currentTimeMillis();

    if ((thisTime - lastTime) >= PERIOD) {
        lastTime = thisTime;

        if(variable) { //If my variable is true
            boolean = true; //Setting my boolean to true
            /**
            *Doing a bunch of things.
            **/
            //I need a delay for about one second here.
            boolean = false; //Setting my boolean to false;
        }
    }
}
like image 42
clstrfsck Avatar answered Nov 15 '22 15:11

clstrfsck


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.

like image 27
ActivX Avatar answered Nov 15 '22 17:11

ActivX