Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

I am new to Java, I want my program to call a method, generate a random number and set the timer for that long, when the timer goes off, call that method again.

Can somebody help me with this?

1 Answer

0 votes
by (13.1k points)

If you want a Timer, you can do something like this:

public class TestClass {

    public long myLong = 1234;

    public static void main(String[] args) {

        final TestClass test = new TestClass();

        Timer timer = new Timer();

        timer.schedule(new TimerTask() {

            @Override

            public void run() {

                test.doStuff();

            }

        }, 0, test.myLong);

    }

    public void doStuff(){

        //do stuff here

    }

}

As you are a beginner I have given you the above example but if you go deeper into Java there are easier ways to do this.

Want to learn Java? Check out the Java certification from Intellipaat.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...