Several uses of Java timers

Source: Internet
Author: User

  1. Package com.lid;
  2. Import Java.util.Calendar;
  3. Import Java.util.Date;
  4. Import Java.util.Timer;
  5. Import Java.util.TimerTask;
  6. Public class Test {
  7. public static void Main (string[] args) {
  8. //timer1 ();
  9. Timer2 ();
  10. //timer3 ();
  11. //timer4 ();
  12. }
  13. ///The first method: sets the specified task to execute the schedule (timertask task, Date time) at the specified times
  14. public static void Timer1 () {
  15. Timer timer = new timer ();
  16. Timer.schedule (new TimerTask () {
  17. public Void Run () {
  18. System.out.println ("-------set to specify task--------");
  19. }
  20. }, 2000); //Set the specified time, 2000 milliseconds
  21. }
  22. ///The second method: sets the execution of a fixed delay peroid for a specified task after a specified delayed delay
  23. //Schedule (timertask task, long delay, long period)
  24. public static void Timer2 () {
  25. Timer timer = new timer ();
  26. Timer.schedule (new TimerTask () {
  27. public Void Run () {
  28. System.out.println ("-------set to specify task--------");
  29. }
  30. }, + , 1000);
  31. }
  32. ///Third method: Sets the execution of a fixed frequency peroid for the specified task tasks after the specified delayed delay.
  33. //Scheduleatfixedrate (timertask task, long delay, long period)
  34. public static void Timer3 () {
  35. Timer timer = new timer ();
  36. Timer.scheduleatfixedrate (new TimerTask () {
  37. public Void Run () {
  38. System.out.println ("-------set to specify task--------");
  39. }
  40. }, + , 2000);
  41. }
  42. //Fourth method: Schedule the specified task to execute at a fixed rate period the specified time Firsttime begins to repeat.
  43. //Timer.scheduleatfixedrate (TimerTask task,date firsttime,long period)
  44. public static void Timer4 () {
  45. Calendar calendar = Calendar.getinstance ();
  46. Calendar.set (Calendar.hour_of_day, 12); //Control time
  47. Calendar.set (Calendar.minute, 0); //Control points
  48. Calendar.set (Calendar.second, 0); //control seconds
  49. Date time = Calendar.gettime (); //arrive at the time of the execution of the task, here is today's 12:00:00
  50. Timer timer = new timer ();
  51. Timer.scheduleatfixedrate (new TimerTask () {
  52. public Void Run () {
  53. System.out.println ("-------set to specify task--------");
  54. }
  55. }, time, + * ( ); This setting will delay the execution of the daily fixed
  56. }
  57. }

Several uses of Java timers

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.