0

I would like to call an existing method which takes a screenshot and execute it every s seconds, depending on the input of the user.

How would it be possible to do that without stopping the program?

Edit: I don't want to call a function n number of times, or after s seconds. Rather, I would like to run it every s seconds without it causing the program to stop.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
A Alw
  • 41
  • 1
  • 6
  • 3
    Possible duplicate of [java: run a function after a specific number of seconds](https://stackoverflow.com/questions/2258066/java-run-a-function-after-a-specific-number-of-seconds) – Mesar ali Nov 17 '19 at 08:12
  • Thank you, but my question is different than the one you have linked. Please check the edit. – A Alw Nov 17 '19 at 08:21

3 Answers3

3

In that case, use "Timer and TimerTask Classes"

import java.util.Timer;
import java.util.TimerTask;

/**
 * Simple demo that uses java.util.Timer to schedule a task 
 * to execute once 5 seconds have passed.
 */

public class Reminder {
    Timer timer;

    public Reminder(int seconds) {
        timer = new Timer();
        timer.schedule(new RemindTask(), seconds*1000);
    }

    class RemindTask extends TimerTask {
        public void run() {
            System.out.println("Time's up!");
            timer.cancel(); //Terminate the timer thread
        }
    }

    public static void main(String args[]) {
        new Reminder(5);
        System.out.println("Task scheduled.");
    }
}

........ The below answer was for the same question which was edited later on ........

In Java 8, You can do this to call a method n times:
But if you put it into a little helper function that takes a couple of parameters

IntStream.range(0, n).forEach(i -> doSomething());

void repeat(int count, Runnable action) {
    IntStream.range(0, count).forEach(i -> action.run());
}
This will enable you to do things like this:

repeat(3, () -> System.out.println("Hello!"));
and also this:

repeat(4, this::doSomething);

forkdbloke
  • 1,505
  • 2
  • 12
  • 29
  • I actually want to call the method every n seconds. The title was a little misguiding, my bad. I have changed it. – A Alw Nov 17 '19 at 04:36
0

As per your description, it's possible duplicate of java: run a function after a specific number of seconds

As per your title, you can use recursion to call it n number of times

int number = 6; // can be anything as per user input
callMethod(number);

//methid implementation
void callMethod(int n) {
//do stuff
if (n>0)
{
callMethod(n-1);
}
}
Mesar ali
  • 1,832
  • 2
  • 16
  • 18
0

You can create a method which will receive an int (n seconds) and then will execute the screenshot method after a Thread.sleep(n*1000).

public void screenShotWithTimer(int n){
    while(true){
        takeScreenShot();
        try{
            Thread.sleep(n*1000);
        }catch(InterruptedException e){}
    }
}
gmonsa39
  • 1
  • 1
  • 1
    [Why `Thread.sleep()` is bad to use](https://stackoverflow.com/questions/17826651/why-thread-sleep-is-bad-to-use) – FailingCoder Nov 17 '19 at 05:43