Java timer timer,timertask randomly generate numbers every time

Source: Internet
Author: User

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
  1. Package com.skyon.gd.test;
  2. Import Java.util.Timer;
  3. /**
  4. *
  5. * @author Sandy
  6. *
  7. */
  8. public class Timercallclient {
  9. public static void Main (string[] args) {
  10. Run ();
  11. }
  12. private static void run () {
  13. Timer timer = new timer ();
  14. Newtimertask timertask = new Newtimertask ();
  15. Execute tasks immediately after the program runs, every 100ms
  16. Timer.schedule (TimerTask, 0, 100);
  17. }
  18. }

Newtimertask.java Code:

Java code
  1. /**
  2. *
  3. */
  4. Package com.skyon.gd.test;
  5. Import Java.util.Random;
  6. Import Java.util.TimerTask;
  7. /**
  8. * @author Sandy
  9. * The TimerTask class is an abstract class
  10. */
  11. Public class Newtimertask extends TimerTask {
  12. @Override
  13. public Void Run () {
  14. Createrandomnumber ();
  15. Createrandomnumberfrommathrandom ();
  16. }
  17. //random number between 1-10 randomly generated using the method in pure math
  18. private void Createrandomnumberfrommathrandom () {
  19. int j= (int) (Math.Round (Math.random () *+1));
  20. System.out.println ("Randomly generated numbers are:" +j);
  21. }
  22. //randomly generate random numbers between 1-10 in random class mode
  23. private void Createrandomnumber () {
  24. Random random=New Random (System.currenttimemillis ());
  25. int value=random.nextint ();
  26. Value=math.abs (value);
  27. value=value%1;
  28. System.out.println ("The newly generated number is:" +value);
  29. }
  30. }

Java timer timer,timertask randomly generate numbers every time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.