I’m a Java beginner and have been futzing around with various solutions to this problem and have gotten myself kind of knotted up. I’ve tried with Threads and then discovered this Timer class and have messed around with it without success so far. If you could post executable code with a main method so I could see it working and start playing around from there, that would be great.
- Launch program
- call
doSomething() - Generate random number and set Timer for that long.
- When Timer goes off, call
doSomething()again.
Probably using this: http://docs.oracle.com/javase/6/docs/api/java/util/Timer.html
If you want to simply use Timer, I would do something like this:
Sorry for the lousy identation.
Also, if you need to schedule execution of code, take a look at Guava Services since it can really make your code much clearer and abstract quite a bit of the boilerplate of creating threads, scheduling, etc.
By the way, I didn’t take the trouble of generating random number, etc, but I think you can figure out how to include that part. I hope this is enough to get you on the right track.
For the record, if you were to use Guava, it would look something like this:
And you would simply create a main that called new CrawlingService.start(); that’s it.