java--Timer Issues

Source: Internet
Author: User
Tags function prototype

Timer issues

Timers are basic components, whether it is User space program development, or kernel space program development, often need to have a timer as the base component support. The implementation of a timer requires the following four basic behaviors: Add timer, cancel timer, timer check, expire execution.
Design a timer and implement the following three basic behaviors, a function prototype is given, you can design data structures and implementations in any programming language, and support as many timers as efficiently as possible:
Add Timer: Perform a target operation after a specific time interval
Input 1:interval timer time, Unit ms
Enter 2:expiryaction target operation
Return: Timer Id
Starttimer (Interval, expiryaction), Timerid
Cancel Timer
Input: Timer Id
Stoptimer (Timerid)
Timer check
The function is called once every 10ms of the system
Pertickbookkeeping ()

Words not much to say, directly on the code:

  1) Timer.java:

1 Importjava.util.ArrayList;2 3  Public classTimer {4 5     Private LongInterval//timer time, Unit MS6     PrivateString expiryaction;//Target Operation7     Private intTimerid;//Timer ID8     Private LongWaitTime;//Timer Wait Time9     Ten     //constructor Function One      PublicTimer () { A          This. WaitTime = 0; -     } -      the     //Adding Timers -      Public intStarttimer (LongInterval, String expiryaction,intID) { -          This. Interval =interval; -          This. expiryaction =expiryaction; +          This. Timerid =ID; -         returnTimerid; +     } A      at     //Cancel Timer -      Public voidStoptimer (intTimerid, arraylist<timer>timer) { - Timer.remove (Timerid); -     } -      -     //Timer Check in      Public voidpertickbookkeeping () { -         if( This. Interval > This. WaitTime) to              This. waitTime + = 10; +         Else{ -SYSTEM.OUT.PRINTLN ("Timer" + This. timerid+ ":" + This. expiryaction); the              This. WaitTime = 0; *         } $     }Panax Notoginseng      -      the      Public LongGetinterval () { +         returninterval; A     } the      Public voidSetInterval (Longinterval) { +          This. Interval =interval; -     } $      PublicString getexpiryaction () { $         returnexpiryaction; -     } -      Public voidsetexpiryaction (String expiryaction) { the          This. expiryaction =expiryaction; -     }Wuyi      Public intGettimerid () { the         returnTimerid; -     } Wu      Public voidSettimerid (intTimerid) { -          This. Timerid =Timerid; About     } $      Public LongGetwaittime () { -         returnWaitTime; -     } -      Public voidSetwaittime (LongwaitTime) { A          This. WaitTime =WaitTime; +     } the}

2) Dotimer.java:

1 Importjava.util.ArrayList;2 ImportJava.util.Iterator;3 4  Public classDotimerextendsThread {5 6     Private StaticArraylist<timer>timerlist;7     8      Public Static voidMain (string[] args) {9         TenTimerlist =NewArraylist<timer>(); OneTimer timer1 =NewTimer (); ATimer1. Starttimer (3000, "I am the first timer, wait 3 seconds", 0); -Timer Timer2 =NewTimer (); -Timer2. Starttimer (4000, "I am the second timer, wait 4 seconds", 1); the Timerlist.add (timer1); - Timerlist.add (timer2); -          -         //Public void Run () {} +         NewThread () { - @Override +              Public voidrun () { A                  while(true){ atIterator<timer> it =timerlist.iterator (); -                      while(It.hasnext ()) { - It.next (). Pertickbookkeeping (); -                     } -                     Try { -Sleep (10); in}Catch(interruptedexception e) { -                         //TODO auto-generated Catch block to e.printstacktrace (); +                     } -                 } the             } * }.start (); $ timer1. Stoptimer (Timer1.gettimerid (), timerlist);Panax Notoginseng          -     } the}

Thank you for your suggestions for improvement, Xiao Li will choose a better way to improve their own procedures!

  

All rights reserved, permit reprint, reprint please indicate source, infringement must investigate!

java--Timer Issues

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.