'Scheduling recurring tasks with java

I am working on a program that logins into a website. It obtains some values and then does certain clicks on the website every 135 minutes or so. value, "obtained_value" is read from the website, which is decremented by some value each click the program makes. I want to run the program till the obtained value is less than 10. Once that happens, I want to pause the program till target time is reached and restart the clicking cycle. I want to do this everytime the target time is reached. I implemented this logic in the following code, but my code remains sleep after target time is reached, instead of restarting the loop. How can I fix this?

        while (true) {
        var remainder = driver.findElement(By.xpath(
                "/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[3]/uni-view[2]/uni-view[2]")).getText();
        var remaining = Double.parseDouble(remainder);
        
        var last_time = driver.findElement(By.xpath(
                "/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[5]/uni-view[3]/uni-view[1]")).getText();

        Calendar date = Calendar.getInstance();
        date.setTime(new SimpleDateFormat("MM-dd HH:mm", Locale.ENGLISH).parse(last_time)); // Parse into Date object
        date.set(Calendar.YEAR, 2022);
        var obtained_value = date.getTime().getTime();

        long current_time = Calendar.getInstance().getTimeInMillis(); // Get time now
        long target_time = obtained_value + 7920000;
        long millis = target_time - System.currentTimeMillis();

        if (millis <= 0) {
            if (remaining > 5) {
                driver.findElement(By.className("orderBtn")).click();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                Thread.sleep(6000);
                driver.findElement(By.xpath("/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[7]/uni-view/uni-view/uni-view[6]/uni-button[2]")).click();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                Thread.sleep(6000);
                driver.findElement(By.xpath("/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[8]/uni-view/uni-view/uni-button")).click();
            }
        }
        try {
           if(millis > 0)
              Thread.sleep(millis);
            else
                continue;
        } catch (InterruptedException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

It makes sense that the code breaks after it reaches the specified time, since Thread.sleep does not accept negative values, but I do not know how to get around this error at the moment. How can resume the loop everytime Thread.sleep(millis) is reached? At the momement, the program remains sleep even after target time is reached.



Solution 1:[1]

Your question is very similar to this one: can you use infinite loop to run a program continuesly using java. The main point is to schedule reoccurring task you should use ScheduledExecutorService that you can obtain from Executors.newScheduledThreadPool(int corePoolSize) medthod. Once you notice that obtained value is less than 10 you should shutdown your ScheduledExecutorService and create a new one with wich you can schedule yourr task to start at your appointed time with your desired intervals.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michael Gantman