Back
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?
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 }}
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.
31k questions
32.8k answers
501 comments
693 users