The 1:java.util.timer class is a tool that a thread uses to schedule tasks to perform later in a background thread. You can schedule a task to execute once, or repeat it periodically.
The 2:timertask class is a task that is scheduled to be performed or performed repeatedly by a timer, because there is a timertask class that is an abstract class that must have its subclasses implemented to complete the scheduled task.
Timercallclient. Java Client code:
Timercallclient.java Code
- Package com.skyon.gd.test;
- Import Java.util.Timer;
- /**
- *
- * @author Sandy
- *
- */
- public class Timercallclient {
- public static void Main (string[] args) {
- Run ();
- }
- private static void run () {
- Timer timer = new timer ();
- Newtimertask timertask = new Newtimertask ();
- Execute tasks immediately after the program runs, every 100ms
- Timer.schedule (TimerTask, 0, 100);
- }
- }
Newtimertask.java Code:
Java code
- /**
- *
- */
- Package com.skyon.gd.test;
- Import Java.util.Random;
- Import Java.util.TimerTask;
- /**
- * @author Sandy
- * The TimerTask class is an abstract class
- */
- Public class Newtimertask extends TimerTask {
- @Override
- public Void Run () {
- Createrandomnumber ();
- Createrandomnumberfrommathrandom ();
- }
- //random number between 1-10 randomly generated using the method in pure math
- private void Createrandomnumberfrommathrandom () {
- int j= (int) (Math.Round (Math.random () *+1));
- System.out.println ("Randomly generated numbers are:" +j);
- }
- //randomly generate random numbers between 1-10 in random class mode
- private void Createrandomnumber () {
- Random random=New Random (System.currenttimemillis ());
- int value=random.nextint ();
- Value=math.abs (value);
- value=value%1;
- System.out.println ("The newly generated number is:" +value);
- }
- }
Java timer timer,timertask randomly generate numbers every time